Skip to content

Commit 7b973eb

Browse files
committed
refactor: reduce code duplication
1 parent aeb7b51 commit 7b973eb

File tree

2 files changed

+24
-70
lines changed

2 files changed

+24
-70
lines changed

Diff for: lib/node_modules/@stdlib/strided/base/unary-by/lib/accessors.js

+12-37
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,46 +66,15 @@
6066
* // => [ 2.0, 4.0, 6.0, 8.0, 10.0 ]
6167
*/
6268
function unaryBy( arrays, shape, strides, accessors, fcn, clbk, thisArg ) {
63-
var xget;
64-
var yset;
65-
var sx;
66-
var sy;
67-
var ix;
68-
var iy;
69-
var x;
70-
var y;
69+
var offsets;
7170
var N;
72-
var v;
73-
var i;
7471

7572
N = shape[ 0 ];
76-
if ( N <= 0 ) {
77-
return;
78-
}
79-
sx = strides[ 0 ];
80-
sy = strides[ 1 ];
81-
if ( sx < 0 ) {
82-
ix = (1-N) * sx;
83-
} else {
84-
ix = 0;
85-
}
86-
if ( sy < 0 ) {
87-
iy = (1-N) * sy;
88-
} else {
89-
iy = 0;
90-
}
91-
x = arrays[ 0 ];
92-
y = arrays[ 1 ];
93-
xget = accessors[ 0 ];
94-
yset = accessors[ 1 ];
95-
for ( i = 0; i < N; i++ ) {
96-
v = clbk.call( thisArg, xget( x, ix ), i, [ ix, iy ], [ x, y ] );
97-
if ( v !== void 0 ) {
98-
yset( y, iy, fcn( v ) );
99-
}
100-
ix += sx;
101-
iy += sy;
102-
}
73+
offsets = [
74+
stride2offset( N, strides[ 0 ] ),
75+
stride2offset( N, strides[ 1 ] )
76+
];
77+
return ndarray( arrays, shape, strides, offsets, accessors, fcn, clbk, thisArg );
10378
}
10479

10580

Diff for: lib/node_modules/@stdlib/strided/base/unary-by/lib/unary.js

+12-33
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( './unary.ndarray.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -51,42 +57,15 @@
5157
* // => [ 2.0, 4.0, 6.0, 8.0, 10.0 ]
5258
*/
5359
function unaryBy( arrays, shape, strides, fcn, clbk, thisArg ) {
54-
var sx;
55-
var sy;
56-
var ix;
57-
var iy;
58-
var x;
59-
var y;
60+
var offsets;
6061
var N;
61-
var v;
62-
var i;
6362

6463
N = shape[ 0 ];
65-
if ( N <= 0 ) {
66-
return;
67-
}
68-
sx = strides[ 0 ];
69-
sy = strides[ 1 ];
70-
if ( sx < 0 ) {
71-
ix = (1-N) * sx;
72-
} else {
73-
ix = 0;
74-
}
75-
if ( sy < 0 ) {
76-
iy = (1-N) * sy;
77-
} else {
78-
iy = 0;
79-
}
80-
x = arrays[ 0 ];
81-
y = arrays[ 1 ];
82-
for ( i = 0; i < N; i++ ) {
83-
v = clbk.call( thisArg, x[ ix ], i, [ ix, iy ], [ x, y ] );
84-
if ( v !== void 0 ) {
85-
y[ iy ] = fcn( v );
86-
}
87-
ix += sx;
88-
iy += sy;
89-
}
64+
offsets = [
65+
stride2offset( N, strides[ 0 ] ),
66+
stride2offset( N, strides[ 1 ] )
67+
];
68+
return ndarray( arrays, shape, strides, offsets, fcn, clbk, thisArg );
9069
}
9170

9271

0 commit comments

Comments
 (0)