Skip to content

Commit 741144c

Browse files
committed
refactor: reduce code duplication
1 parent 1600510 commit 741144c

File tree

2 files changed

+14
-52
lines changed

2 files changed

+14
-52
lines changed

lib/node_modules/@stdlib/strided/base/map-by/lib/accessors.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './accessors.ndarray.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -60,32 +66,7 @@
6066
* // => [ 2.0, 4.0, 6.0, 8.0, 10.0 ]
6167
*/
6268
function mapBy( N, x, strideX, xget, y, strideY, yset, fcn, clbk, thisArg ) {
63-
var ix;
64-
var iy;
65-
var v;
66-
var i;
67-
if ( N <= 0 ) {
68-
return y;
69-
}
70-
if ( strideX < 0 ) {
71-
ix = (1-N) * strideX;
72-
} else {
73-
ix = 0;
74-
}
75-
if ( strideY < 0 ) {
76-
iy = (1-N) * strideY;
77-
} else {
78-
iy = 0;
79-
}
80-
for ( i = 0; i < N; i++ ) {
81-
v = clbk.call( thisArg, xget( x, ix ), i, [ ix, iy ], [ x, y ] );
82-
if ( v !== void 0 ) {
83-
yset( y, iy, fcn( v ) );
84-
}
85-
ix += strideX;
86-
iy += strideY;
87-
}
88-
return y;
69+
return ndarray( N, x, strideX, stride2offset( N, strideX ), xget, y, strideY, stride2offset( N, strideY ), yset, fcn, clbk, thisArg ); // eslint-disable-line max-len
8970
}
9071

9172

lib/node_modules/@stdlib/strided/base/map-by/lib/map.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './map.ndarray.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -50,32 +56,7 @@
5056
* // => [ 2.0, 4.0, 6.0, 8.0, 10.0 ]
5157
*/
5258
function mapBy( N, x, strideX, y, strideY, fcn, clbk, thisArg ) {
53-
var ix;
54-
var iy;
55-
var v;
56-
var i;
57-
if ( N <= 0 ) {
58-
return y;
59-
}
60-
if ( strideX < 0 ) {
61-
ix = (1-N) * strideX;
62-
} else {
63-
ix = 0;
64-
}
65-
if ( strideY < 0 ) {
66-
iy = (1-N) * strideY;
67-
} else {
68-
iy = 0;
69-
}
70-
for ( i = 0; i < N; i++ ) {
71-
v = clbk.call( thisArg, x[ ix ], i, [ ix, iy ], [ x, y ] );
72-
if ( v !== void 0 ) {
73-
y[ iy ] = fcn( v );
74-
}
75-
ix += strideX;
76-
iy += strideY;
77-
}
78-
return y;
59+
return ndarray( N, x, strideX, stride2offset( N, strideX ), y, strideY, stride2offset( N, strideY ), fcn, clbk, thisArg ); // eslint-disable-line max-len
7960
}
8061

8162

0 commit comments

Comments
 (0)