Skip to content

Commit

Permalink
refactor: reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jul 6, 2024
1 parent e47b1be commit cfcce81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 44 deletions.
31 changes: 8 additions & 23 deletions lib/node_modules/@stdlib/strided/base/nullary/lib/accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// MODULES //

var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './accessors.ndarray.js' );


// MAIN //

/**
Expand Down Expand Up @@ -53,29 +59,8 @@
* // => <Float64Array>[ 6.0, 6.0, 6.0, 6.0, 6.0 ]
*/
function nullary( arrays, shape, strides, accessors, fcn ) {
var xset;
var sx;
var ix;
var x;
var N;
var i;

N = shape[ 0 ];
if ( N <= 0 ) {
return;
}
sx = strides[ 0 ];
if ( sx < 0 ) {
ix = (1-N) * sx;
} else {
ix = 0;
}
x = arrays[ 0 ];
xset = accessors[ 0 ];
for ( i = 0; i < N; i++ ) {
xset( x, ix, fcn() );
ix += sx;
}
var offsets = [ stride2offset( shape[ 0 ], strides[ 0 ] ) ];
return ndarray( arrays, shape, strides, offsets, accessors, fcn );
}


Expand Down
29 changes: 8 additions & 21 deletions lib/node_modules/@stdlib/strided/base/nullary/lib/nullary.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// MODULES //

var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './nullary.ndarray.js' );


// MAIN //

/**
Expand Down Expand Up @@ -48,27 +54,8 @@
* // => <Float64Array>[ 3.0, 3.0, 3.0, 3.0, 3.0 ]
*/
function nullary( arrays, shape, strides, fcn ) {
var sx;
var ix;
var x;
var N;
var i;

N = shape[ 0 ];
if ( N <= 0 ) {
return;
}
sx = strides[ 0 ];
if ( sx < 0 ) {
ix = (1-N) * sx;
} else {
ix = 0;
}
x = arrays[ 0 ];
for ( i = 0; i < N; i++ ) {
x[ ix ] = fcn();
ix += sx;
}
var offsets = [ stride2offset( shape[ 0 ], strides[ 0 ] ) ];
return ndarray( arrays, shape, strides, offsets, fcn );
}


Expand Down

0 comments on commit cfcce81

Please sign in to comment.