Skip to content

Commit 67bdae8

Browse files
committed
refactor: apply suggestions from code review
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent ec79858 commit 67bdae8

File tree

2 files changed

+76
-122
lines changed

2 files changed

+76
-122
lines changed

lib/node_modules/@stdlib/ndarray/find/lib/assign.js

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ var getSentinelValue = require( './sentinel.js' );
4545
* Finds the first elements which pass a test implemented by a predicate function along one or more ndarray dimensions and assigns results to a provided output ndarray.
4646
*
4747
* @param {ndarray} x - input ndarray
48-
* @param {ndarray} y - output ndarray
49-
* @param {*|ndarray} [sentinelValue] - sentinel value
48+
* @param {(*|ndarray)} [sentinelValue] - sentinel value
49+
* @param {ndarray} out - output ndarray
5050
* @param {Options} [options] - function options
5151
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction
5252
* @param {Function} predicate - predicate function
5353
* @param {*} [thisArg] - predicate function execution context
5454
* @throws {TypeError} first argument must be an ndarray-like object
55-
* @throws {TypeError} second argument must be an ndarray-like object
55+
* @throws {TypeError} third argument must be an ndarray-like object
5656
* @throws {TypeError} options argument must be an object
5757
* @throws {TypeError} callback argument must be a function
5858
* @throws {Error} must provide valid options
@@ -68,101 +68,81 @@ var getSentinelValue = require( './sentinel.js' );
6868
* // returns <ndarray>
6969
*
7070
* // Create an output ndarray:
71-
* var y = empty( [], {
71+
* var out = empty( [], {
7272
* 'dtype': x.dtype
7373
* });
7474
*
7575
* // Perform reduction:
76-
* var out = assign( x, y, isEven );
76+
* var result = assign( x, -1, out, isEven );
7777
* // returns <ndarray>
7878
*
7979
* var v = out.get();
8080
* // returns 2.0
8181
*/
82-
function assign( x, y ) {
82+
function assign( x ) {
8383
var sentinelValue;
8484
var predicate;
8585
var thisArg;
8686
var options;
8787
var nargs;
8888
var opts;
8989
var err;
90+
var out;
9091
var sv;
92+
var pi;
9193
var N;
94+
var i;
9295

9396
nargs = arguments.length;
9497
if ( !isndarrayLike( x ) ) {
9598
throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
9699
}
97-
if ( !isndarrayLike( y ) ) {
98-
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', y ) );
100+
pi = -1;
101+
for ( i = 1; i < nargs; i++ ) {
102+
if ( isFunction( arguments[ i ] ) ) {
103+
pi = i;
104+
break;
105+
}
99106
}
100-
101-
if ( nargs === 3 ) {
102-
// assign( x, y, predicate )
107+
if ( pi === -1 ) {
108+
throw new TypeError( 'invalid arguments. Must provide a predicate function. Value: `%s`.', arguments[ i ] );
109+
} else if ( pi === 2 ) { // assign( x, out, predicate [, thisArg] )
110+
out = arguments[ 1 ];
103111
predicate = arguments[ 2 ];
104-
if ( !isFunction( predicate ) ) {
105-
throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) );
106-
}
107-
} else if ( nargs === 4 ) {
108-
if ( isFunction( arguments[ 2 ] ) ) {
109-
// assign( x, y, predicate, thisArg )
110-
predicate = arguments[ 2 ];
112+
if ( nargs === 4 ) {
111113
thisArg = arguments[ 3 ];
112-
} else if ( isPlainObject( arguments[ 2 ] ) ) {
113-
// assign( x, y, options, predicate )
114-
options = arguments[ 2 ];
115-
predicate = arguments[ 3 ];
116-
if ( !isFunction( predicate ) ) {
117-
throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) );
118-
}
119-
} else {
120-
// assign( x, y, sentinel, predicate )
121-
sentinelValue = arguments[ 2 ];
122-
predicate = arguments[ 3 ];
123-
if ( !isFunction( predicate ) ) {
124-
throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) );
125-
}
126114
}
127-
} else if ( nargs === 5 ) {
128-
if ( isPlainObject( arguments[ 2 ] ) ) {
129-
// assign( x, y, options, predicate, thisArg )
115+
} else if ( pi === 3 ) {
116+
if ( isPlainObject( arguments[ 2 ] ) ) { // assign( x, out, options, predicate [, thisArg] )
117+
out = arguments[ 1 ];
130118
options = arguments[ 2 ];
131119
predicate = arguments[ 3 ];
132-
thisArg = arguments[ 4 ];
133-
if ( !isFunction( predicate ) ) {
134-
throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) );
120+
if ( nargs === 5 ) {
121+
thisArg = arguments[ 4 ];
135122
}
136-
} else if ( isPlainObject( arguments[ 3 ] ) ) {
137-
// assign( x, y, sentinel, options, predicate )
138-
sentinelValue = arguments[ 2 ];
139-
options = arguments[ 3 ];
140-
predicate = arguments[ 4 ];
141-
if ( !isFunction( predicate ) ) {
142-
throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', predicate ) );
143-
}
144-
} else {
145-
// assign( x, y, sentinel, predicate, thisArg )
146-
sentinelValue = arguments[ 2 ];
123+
} else { // assign( x, sentinel, out, predicate [, thisArg] )
124+
sentinelValue = arguments[ 1 ];
125+
out = arguments[ 2 ];
147126
predicate = arguments[ 3 ];
148-
thisArg = arguments[ 4 ];
149-
if ( !isFunction( predicate ) ) {
150-
throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) );
127+
if ( nargs === 5 ) {
128+
thisArg = arguments[ 4 ];
151129
}
152130
}
153-
} else if ( nargs === 6 ) {
154-
// assign( x, y, sentinel, options, predicate, thisArg )
155-
sentinelValue = arguments[ 2 ];
131+
} else { // assign( x, sentinel, out, options, predicate [, thisArg] )
132+
sentinelValue = arguments[ 1 ];
133+
out = arguments[ 2 ];
156134
options = arguments[ 3 ];
157135
predicate = arguments[ 4 ];
158-
thisArg = arguments[ 5 ];
159-
if ( !isFunction( predicate ) ) {
160-
throw new TypeError( format( 'invalid argument. Fifth argument must be a function. Value: `%s`.', predicate ) );
136+
if ( nargs === 6 ) {
137+
thisArg = arguments[ 5 ];
161138
}
162-
} else {
163-
throw new Error( 'invalid number of arguments.' );
164139
}
165-
140+
if ( !isndarrayLike( out ) ) {
141+
throw new TypeError( format( 'invalid argument. Output argument must be an ndarray-like object. Value: `%s`.', out ) );
142+
}
143+
if ( options && !isPlainObject( options ) ) {
144+
throw new TypeError( format( 'invalid argument. Options argument must be a plain object. Value: `%s`.', options ) );
145+
}
166146
N = ndims( x );
167147

168148
// Validate options
@@ -186,19 +166,14 @@ function assign( x, y ) {
186166

187167
// Broadcast sentinel value to match the output array shape:
188168
if ( isndarrayLike( sentinelValue ) ) {
189-
try {
190-
sv = maybeBroadcastArray( sentinelValue, getShape( y ) );
191-
} catch ( err ) { // eslint-disable-line no-unused-vars
192-
throw new Error( 'invalid argument. Sentinel value must be broadcast-compatible with the output array.' );
193-
}
169+
sv = maybeBroadcastArray( sentinelValue, getShape( out ) );
194170
} else {
195-
sv = broadcastScalar( sentinelValue, getDtype( x ), getShape( y ), getOrder( y ) );
171+
sv = broadcastScalar( sentinelValue, getDtype( x ), getShape( out ), getOrder( out ) ); // eslint-disable-line max-len
196172
}
197173

198174
// Perform the reduction:
199-
unaryReduceSubarrayBy( base, [ x, y, sv ], opts.dims, predicate, thisArg );
200-
201-
return y;
175+
unaryReduceSubarrayBy( base, [ x, out, sv ], opts.dims, predicate, thisArg ); // eslint-disable-line max-len
176+
return out;
202177
}
203178

204179

lib/node_modules/@stdlib/ndarray/find/lib/main.js

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -89,76 +89,55 @@ function find( x ) { // eslint-disable-line stdlib/no-redeclare
8989
var shx;
9090
var shy;
9191
var ord;
92+
var out;
9293
var sv;
94+
var pi;
9395
var N;
94-
var y;
96+
var i;
9597

9698
nargs = arguments.length;
9799
if ( !isndarrayLike( x ) ) {
98100
throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
99101
}
100-
101-
if ( nargs <= 2 ) {
102-
// find( x, predicate )
103-
predicate = arguments[ 1 ];
104-
if ( !isFunction( predicate ) ) {
105-
throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', predicate ) );
102+
pi = -1;
103+
for ( i = 1; i < nargs; i++ ) {
104+
if ( isFunction( arguments[ i ] ) ) {
105+
pi = i;
106+
break;
106107
}
107-
} else if ( nargs === 3 ) {
108-
if ( isFunction( arguments[ 1 ] ) ) {
109-
// find( x, predicate, thisArg )
110-
predicate = arguments[ 1 ];
108+
}
109+
if ( pi === -1 ) {
110+
throw new TypeError( format( 'invalid arguments. Must provide a predicate function. Value: `%s`.', arguments[ i ] ) );
111+
} else if ( pi === 1 ) { // find( x, predicate [, thisArg] )
112+
predicate = arguments[ 1 ];
113+
if ( nargs === 3 ) {
111114
thisArg = arguments[ 2 ];
112-
} else if ( isPlainObject( arguments[ 1 ] ) ) {
113-
options = arguments[ 1 ];
114-
predicate = arguments[ 2 ];
115-
if ( !isFunction( predicate ) ) {
116-
throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) );
117-
}
118-
} else {
119-
sentinelValue = arguments[ 1 ];
120-
predicate = arguments[ 2 ];
121-
if ( !isFunction( predicate ) ) {
122-
throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) );
123-
}
124115
}
125-
} else if ( nargs === 4 ) {
126-
if ( isPlainObject( arguments[ 1 ] ) ) {
127-
// find( x, options, predicate, thisArg )
116+
} else if ( pi === 2 ) {
117+
if ( isPlainObject( arguments[ 1 ] ) ) { // find( x, options, predicate [, thisArg] )
128118
options = arguments[ 1 ];
129119
predicate = arguments[ 2 ];
130-
thisArg = arguments[ 3 ];
131-
if ( !isFunction( predicate ) ) {
132-
throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) );
120+
if ( nargs === 4 ) {
121+
thisArg = arguments[ 3 ];
133122
}
134-
} else if ( isPlainObject( arguments[ 2 ] ) ) {
135-
// find( x, sentinel, options, predicate )
136-
sentinelValue = arguments[ 1 ];
137-
options = arguments[ 2 ];
138-
predicate = arguments[ 3 ];
139-
if ( !isFunction( predicate ) ) {
140-
throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) );
141-
}
142-
} else {
143-
// find( x, sentinel, predicate, thisArg )
123+
} else { // find( x, sentinel, predicate [, thisArg] )
144124
sentinelValue = arguments[ 1 ];
145125
predicate = arguments[ 2 ];
146-
thisArg = arguments[ 3 ];
147-
if ( !isFunction( predicate ) ) {
148-
throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) );
126+
if ( nargs === 4 ) {
127+
thisArg = arguments[ 3 ];
149128
}
150129
}
151-
} else { // nargs >= 5
152-
// find( x, sentinel, options, predicate, thisArg )
130+
} else { // find( x, sentinel, options, predicate [, thisArg] )
153131
sentinelValue = arguments[ 1 ];
154132
options = arguments[ 2 ];
155133
predicate = arguments[ 3 ];
156-
thisArg = arguments[ 4 ];
157-
if ( !isFunction( predicate ) ) {
158-
throw new TypeError( format( 'invalid argument. Fourth argument must be a function. Value: `%s`.', predicate ) );
134+
if ( nargs === 5 ) {
135+
thisArg = arguments[ 4 ];
159136
}
160137
}
161-
138+
if ( options && !isPlainObject( options ) ) {
139+
throw new TypeError( format( 'invalid argument. Options argument must be a plain object. Value: `%s`.', options ) );
140+
}
162141
shx = getShape( x );
163142
N = shx.length;
164143

@@ -197,19 +176,19 @@ function find( x ) { // eslint-disable-line stdlib/no-redeclare
197176
}
198177

199178
// Initialize an output array whose shape matches that of the non-reduced dimensions and which has the same dtype as the input array:
200-
y = empty( shy, {
179+
out = empty( shy, {
201180
'dtype': getDtype( x ),
202181
'order': ord
203182
});
204183

205184
// Perform the reduction:
206-
unaryReduceSubarrayBy( base, [ x, y, sv ], opts.dims, predicate, thisArg );
185+
unaryReduceSubarrayBy( base, [ x, out, sv ], opts.dims, predicate, thisArg ); // eslint-disable-line max-len
207186

208187
// Check whether we need to reinsert singleton dimensions which can be useful for broadcasting the returned output array to the shape of the original input array...
209188
if ( opts.keepdims ) {
210-
y = spreadDimensions( N, y, idx );
189+
out = spreadDimensions( N, out, idx );
211190
}
212-
return y;
191+
return out;
213192
}
214193

215194

0 commit comments

Comments
 (0)