Skip to content

Commit cfcce81

Browse files
committed
refactor: reduce code duplication
1 parent e47b1be commit cfcce81

File tree

2 files changed

+16
-44
lines changed

2 files changed

+16
-44
lines changed

lib/node_modules/@stdlib/strided/base/nullary/lib/accessors.js

Lines changed: 8 additions & 23 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
/**
@@ -53,29 +59,8 @@
5359
* // => <Float64Array>[ 6.0, 6.0, 6.0, 6.0, 6.0 ]
5460
*/
5561
function nullary( arrays, shape, strides, accessors, fcn ) {
56-
var xset;
57-
var sx;
58-
var ix;
59-
var x;
60-
var N;
61-
var i;
62-
63-
N = shape[ 0 ];
64-
if ( N <= 0 ) {
65-
return;
66-
}
67-
sx = strides[ 0 ];
68-
if ( sx < 0 ) {
69-
ix = (1-N) * sx;
70-
} else {
71-
ix = 0;
72-
}
73-
x = arrays[ 0 ];
74-
xset = accessors[ 0 ];
75-
for ( i = 0; i < N; i++ ) {
76-
xset( x, ix, fcn() );
77-
ix += sx;
78-
}
62+
var offsets = [ stride2offset( shape[ 0 ], strides[ 0 ] ) ];
63+
return ndarray( arrays, shape, strides, offsets, accessors, fcn );
7964
}
8065

8166

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

Lines changed: 8 additions & 21 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( './nullary.ndarray.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -48,27 +54,8 @@
4854
* // => <Float64Array>[ 3.0, 3.0, 3.0, 3.0, 3.0 ]
4955
*/
5056
function nullary( arrays, shape, strides, fcn ) {
51-
var sx;
52-
var ix;
53-
var x;
54-
var N;
55-
var i;
56-
57-
N = shape[ 0 ];
58-
if ( N <= 0 ) {
59-
return;
60-
}
61-
sx = strides[ 0 ];
62-
if ( sx < 0 ) {
63-
ix = (1-N) * sx;
64-
} else {
65-
ix = 0;
66-
}
67-
x = arrays[ 0 ];
68-
for ( i = 0; i < N; i++ ) {
69-
x[ ix ] = fcn();
70-
ix += sx;
71-
}
57+
var offsets = [ stride2offset( shape[ 0 ], strides[ 0 ] ) ];
58+
return ndarray( arrays, shape, strides, offsets, fcn );
7259
}
7360

7461

0 commit comments

Comments
 (0)