Skip to content

Commit aeb7b51

Browse files
committed
refactor: reduce code duplication
1 parent 6249bfd commit aeb7b51

File tree

2 files changed

+24
-62
lines changed

2 files changed

+24
-62
lines changed

lib/node_modules/@stdlib/strided/base/unary/lib/accessors.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( './accessors.ndarray.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -58,42 +64,15 @@
5864
* // => <Float64Array>[ 20.0, 40.0, 60.0, 80.0, 100.0 ]
5965
*/
6066
function unary( arrays, shape, strides, accessors, fcn ) {
61-
var xget;
62-
var yset;
63-
var sx;
64-
var sy;
65-
var ix;
66-
var iy;
67-
var x;
68-
var y;
67+
var offsets;
6968
var N;
70-
var i;
7169

7270
N = shape[ 0 ];
73-
if ( N <= 0 ) {
74-
return;
75-
}
76-
sx = strides[ 0 ];
77-
sy = strides[ 1 ];
78-
if ( sx < 0 ) {
79-
ix = (1-N) * sx;
80-
} else {
81-
ix = 0;
82-
}
83-
if ( sy < 0 ) {
84-
iy = (1-N) * sy;
85-
} else {
86-
iy = 0;
87-
}
88-
x = arrays[ 0 ];
89-
y = arrays[ 1 ];
90-
xget = accessors[ 0 ];
91-
yset = accessors[ 1 ];
92-
for ( i = 0; i < N; i++ ) {
93-
yset( y, iy, fcn( xget( x, ix ) ) );
94-
ix += sx;
95-
iy += sy;
96-
}
71+
offsets = [
72+
stride2offset( N, strides[ 0 ] ),
73+
stride2offset( N, strides[ 1 ] )
74+
];
75+
return ndarray( arrays, shape, strides, offsets, accessors, fcn );
9776
}
9877

9978

lib/node_modules/@stdlib/strided/base/unary/lib/unary.js

+12-29
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
/**
@@ -49,38 +55,15 @@
4955
* // => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0 ]
5056
*/
5157
function unary( arrays, shape, strides, fcn ) {
52-
var sx;
53-
var sy;
54-
var ix;
55-
var iy;
56-
var x;
57-
var y;
58+
var offsets;
5859
var N;
59-
var i;
6060

6161
N = shape[ 0 ];
62-
if ( N <= 0 ) {
63-
return;
64-
}
65-
sx = strides[ 0 ];
66-
sy = strides[ 1 ];
67-
if ( sx < 0 ) {
68-
ix = (1-N) * sx;
69-
} else {
70-
ix = 0;
71-
}
72-
if ( sy < 0 ) {
73-
iy = (1-N) * sy;
74-
} else {
75-
iy = 0;
76-
}
77-
x = arrays[ 0 ];
78-
y = arrays[ 1 ];
79-
for ( i = 0; i < N; i++ ) {
80-
y[ iy ] = fcn( x[ ix ] );
81-
ix += sx;
82-
iy += sy;
83-
}
62+
offsets = [
63+
stride2offset( N, strides[ 0 ] ),
64+
stride2offset( N, strides[ 1 ] )
65+
];
66+
return ndarray( arrays, shape, strides, offsets, fcn );
8467
}
8568

8669

0 commit comments

Comments
 (0)