Skip to content

Commit a04d0dd

Browse files
committed
Refactor to ignore undefined return values
This allows replicating `msk*` interface behavior.
1 parent 3bec1e8 commit a04d0dd

File tree

7 files changed

+96
-14
lines changed

7 files changed

+96
-14
lines changed

lib/node_modules/@stdlib/stats/base/min-by/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ var v = minBy.ndarray( 3, x, 1, x.length-3, accessor );
161161

162162
- If `N <= 0`, both functions return `NaN`.
163163
- A provided callback function should return a numeric value.
164+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
164165
- When possible, prefer using [`dmin`][@stdlib/stats/base/dmin], [`smin`][@stdlib/stats/base/smin], and/or [`min`][@stdlib/stats/base/min], as, depending on the environment, these interfaces are likely to be significantly more performant.
165166

166167
</section>

lib/node_modules/@stdlib/stats/base/min-by/docs/repl.txt

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
The callback function should return a numeric value.
2121

22+
If the callback function does not return any value (or equivalently,
23+
explicitly returns `undefined`), the value is ignored.
24+
2225
Parameters
2326
----------
2427
N: integer

lib/node_modules/@stdlib/stats/base/min-by/docs/types/index.d.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ import { Collection } from '@stdlib/types/object';
2727
*
2828
* @returns accessed value
2929
*/
30-
type Nullary = () => number;
30+
type Nullary = () => number | void;
3131

3232
/**
3333
* Returns an accessed value.
3434
*
3535
* @param value - array element
3636
* @returns accessed value
3737
*/
38-
type Unary = ( value: any ) => number;
38+
type Unary = ( value: any ) => number | void;
3939

4040
/**
4141
* Returns an accessed value.
@@ -44,7 +44,7 @@ type Unary = ( value: any ) => number;
4444
* @param aidx - array index
4545
* @returns accessed value
4646
*/
47-
type Binary = ( value: any, aidx: number ) => number;
47+
type Binary = ( value: any, aidx: number ) => number | void;
4848

4949
/**
5050
* Returns an accessed value.
@@ -54,7 +54,7 @@ type Binary = ( value: any, aidx: number ) => number;
5454
* @param sidx - strided index (offset + aidx*stride)
5555
* @returns accessed value
5656
*/
57-
type Tertiary = ( value: any, aidx: number, sidx: number ) => number;
57+
type Tertiary = ( value: any, aidx: number, sidx: number ) => number | void;
5858

5959
/**
6060
* Returns an accessed value.
@@ -65,7 +65,7 @@ type Tertiary = ( value: any, aidx: number, sidx: number ) => number;
6565
* @param array - input array
6666
* @returns accessed value
6767
*/
68-
type Quaternary = ( value: any, aidx: number, sidx: number, array: Collection ) => number; // tslint-disable-line max-line-length
68+
type Quaternary = ( value: any, aidx: number, sidx: number, array: Collection ) => number | void; // tslint-disable-line max-line-length
6969

7070
/**
7171
* Returns an accessed value.
@@ -94,7 +94,7 @@ interface Routine {
9494
* - `sidx`: strided index (offset + aidx*stride)
9595
* - `array`: input array
9696
*
97-
* - The callback function should return a numeric value.
97+
* - The callback function should return a numeric value. If the callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored.
9898
*
9999
* @param N - number of indexed elements
100100
* @param x - input array
@@ -127,7 +127,7 @@ interface Routine {
127127
* - `sidx`: strided index (offset + aidx*stride)
128128
* - `array`: input array
129129
*
130-
* - The callback function should return a numeric value.
130+
* - The callback function should return a numeric value. If the callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored.
131131
*
132132
* @param N - number of indexed elements
133133
* @param x - input array
@@ -162,7 +162,7 @@ interface Routine {
162162
* - `sidx`: strided index (offset + aidx*stride)
163163
* - `array`: input array
164164
*
165-
* - The callback function should return a numeric value.
165+
* - The callback function should return a numeric value. If the callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored.
166166
*
167167
* @param N - number of indexed elements
168168
* @param x - input array

lib/node_modules/@stdlib/stats/base/min-by/lib/min_by.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,34 @@ function minBy( N, x, stride, clbk, thisArg ) {
5656
return NaN;
5757
}
5858
if ( N === 1 || stride === 0 ) {
59-
return clbk.call( thisArg, x[ 0 ], 0, 0, x );
59+
v = clbk.call( thisArg, x[ 0 ], 0, 0, x );
60+
if ( v === void 0 ) {
61+
return NaN;
62+
}
63+
return v;
6064
}
6165
if ( stride < 0 ) {
6266
ix = (1-N) * stride;
6367
} else {
6468
ix = 0;
6569
}
66-
min = clbk.call( thisArg, x[ ix ], 0, ix, x );
67-
for ( i = 1; i < N; i++ ) {
70+
for ( i = 0; i < N; i++ ) {
71+
min = clbk.call( thisArg, x[ ix ], i, ix, x );
72+
if ( min !== void 0 ) {
73+
break;
74+
}
75+
ix += stride;
76+
}
77+
if ( i === N ) {
78+
return NaN;
79+
}
80+
i += 1;
81+
for ( i; i < N; i++ ) {
6882
ix += stride;
6983
v = clbk.call( thisArg, x[ ix ], i, ix, x );
84+
if ( v === void 0 ) {
85+
continue;
86+
}
7087
if ( isnan( v ) ) {
7188
return v;
7289
}

lib/node_modules/@stdlib/stats/base/min-by/lib/ndarray.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,30 @@ function minBy( N, x, stride, offset, clbk, thisArg ) {
5757
return NaN;
5858
}
5959
if ( N === 1 || stride === 0 ) {
60-
return clbk.call( thisArg, x[ 0 ], 0, 0, x );
60+
v = clbk.call( thisArg, x[ 0 ], 0, 0, x );
61+
if ( v === void 0 ) {
62+
return NaN;
63+
}
64+
return v;
6165
}
6266
ix = offset;
63-
min = clbk.call( thisArg, x[ ix ], 0, ix, x );
64-
for ( i = 1; i < N; i++ ) {
67+
for ( i = 0; i < N; i++ ) {
68+
min = clbk.call( thisArg, x[ ix ], i, ix, x );
69+
if ( min !== void 0 ) {
70+
break;
71+
}
72+
ix += stride;
73+
}
74+
if ( i === N ) {
75+
return NaN;
76+
}
77+
i += 1;
78+
for ( i; i < N; i++ ) {
6579
ix += stride;
6680
v = clbk.call( thisArg, x[ ix ], i, ix, x );
81+
if ( v === void 0 ) {
82+
continue;
83+
}
6784
if ( isnan( v ) ) {
6885
return v;
6986
}

lib/node_modules/@stdlib/stats/base/min-by/test/test.min_by.js

+22
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var minBy = require( './../lib/min_by.js' );
3131
// FUNCTIONS //
3232

3333
function accessor( v ) {
34+
if ( v === void 0 ) {
35+
return;
36+
}
3437
return v * 2.0;
3538
}
3639

@@ -72,6 +75,15 @@ tape( 'the function calculates the minimum value of a strided array via a callba
7275
v = minBy( x.length, x, 1, accessor );
7376
t.strictEqual( isnan( v ), true, 'returns expected value' );
7477

78+
x = new Array( 5 ); // sparse array
79+
v = minBy( x.length, x, 1, accessor );
80+
t.strictEqual( isnan( v ), true, 'returns expected value' );
81+
82+
x = new Array( 5 ); // sparse array
83+
x[ 2 ] = 1.0;
84+
v = minBy( x.length, x, 1, accessor );
85+
t.strictEqual( v, 2.0, 'returns expected value' );
86+
7587
t.end();
7688
});
7789

@@ -99,6 +111,11 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first
99111
v = minBy( 1, x, 1, accessor );
100112
t.strictEqual( v, 2.0, 'returns expected value' );
101113

114+
x = new Array( 1 ); // sparse array
115+
116+
v = minBy( 1, x, 1, accessor );
117+
t.strictEqual( isnan( v ), true, 'returns expected value' );
118+
102119
t.end();
103120
});
104121

@@ -157,6 +174,11 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f
157174
v = minBy( x.length, x, 0, accessor );
158175
t.strictEqual( v, 2.0, 'returns expected value' );
159176

177+
x = new Array( 1 ); // sparse array
178+
179+
v = minBy( 1, x, 0, accessor );
180+
t.strictEqual( isnan( v ), true, 'returns expected value' );
181+
160182
t.end();
161183
});
162184

lib/node_modules/@stdlib/stats/base/min-by/test/test.ndarray.js

+22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ var minBy = require( './../lib/ndarray.js' );
3030
// FUNCTIONS //
3131

3232
function accessor( v ) {
33+
if ( v === void 0 ) {
34+
return;
35+
}
3336
return v * 2.0;
3437
}
3538

@@ -71,6 +74,15 @@ tape( 'the function calculates the minimum value of a strided array via a callba
7174
v = minBy( x.length, x, 1, 0, accessor );
7275
t.strictEqual( isnan( v ), true, 'returns expected value' );
7376

77+
x = new Array( 5 ); // sparse array
78+
v = minBy( x.length, x, 1, 0, accessor );
79+
t.strictEqual( isnan( v ), true, 'returns expected value' );
80+
81+
x = new Array( 5 ); // sparse array
82+
x[ 2 ] = 1.0;
83+
v = minBy( x.length, x, 1, 0, accessor );
84+
t.strictEqual( v, 2.0, 'returns expected value' );
85+
7486
t.end();
7587
});
7688

@@ -98,6 +110,11 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first
98110
v = minBy( 1, x, 1, 0, accessor );
99111
t.strictEqual( v, 2.0, 'returns expected value' );
100112

113+
x = new Array( 1 ); // sparse array
114+
115+
v = minBy( 1, x, 1, 0, accessor );
116+
t.strictEqual( isnan( v ), true, 'returns expected value' );
117+
101118
t.end();
102119
});
103120

@@ -156,6 +173,11 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f
156173
v = minBy( x.length, x, 0, 0, accessor );
157174
t.strictEqual( v, 2.0, 'returns expected value' );
158175

176+
x = new Array( 1 ); // sparse array
177+
178+
v = minBy( 1, x, 0, 0, accessor );
179+
t.strictEqual( isnan( v ), true, 'returns expected value' );
180+
159181
t.end();
160182
});
161183

0 commit comments

Comments
 (0)