Skip to content

Commit 5501d03

Browse files
feat: add stats/array/min-by
PR-URL: #7155 Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 7475c9d commit 5501d03

File tree

10 files changed

+1011
-0
lines changed

10 files changed

+1011
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# minBy
22+
23+
> Calculate the minimum value of an array via a callback function.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var minBy = require( '@stdlib/stats/array/min-by' );
37+
```
38+
39+
#### minBy( x, clbk\[, thisArg] )
40+
41+
Computes the minimum value of an array via a callback function.
42+
43+
```javascript
44+
function accessor( v ) {
45+
return v * 2.0;
46+
}
47+
48+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
49+
50+
var v = minBy( x, accessor );
51+
// returns -10.0
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **x**: input array.
57+
- **clbk**: callback function.
58+
- **thisArg**: execution context (_optional_).
59+
60+
The invoked callback is provided three arguments:
61+
62+
- **value**: current array element.
63+
- **index**: current array index.
64+
- **array**: input array.
65+
66+
To set the callback execution context, provide a `thisArg`.
67+
68+
```javascript
69+
function accessor( v ) {
70+
this.count += 1;
71+
return v * 2.0;
72+
}
73+
74+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
75+
76+
var context = {
77+
'count': 0
78+
};
79+
80+
var v = minBy( x, accessor, context );
81+
// returns -10.0
82+
83+
var cnt = context.count;
84+
// returns 8
85+
```
86+
87+
</section>
88+
89+
<!-- /.usage -->
90+
91+
<section class="notes">
92+
93+
## Notes
94+
95+
- If provided an empty array, the function returns `NaN`.
96+
- A provided callback function should return a numeric value.
97+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
98+
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<section class="examples">
105+
106+
## Examples
107+
108+
<!-- eslint no-undef: "error" -->
109+
110+
```javascript
111+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
112+
var minBy = require( '@stdlib/stats/array/min-by' );
113+
114+
function accessor( v ) {
115+
return v * 2.0;
116+
}
117+
118+
var x = discreteUniform( 10, -50, 50, {
119+
'dtype': 'float64'
120+
});
121+
console.log( x );
122+
123+
var v = minBy( x, accessor );
124+
console.log( v );
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
</section>
136+
137+
<!-- /.related -->
138+
139+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
140+
141+
<section class="links">
142+
143+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
144+
145+
</section>
146+
147+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var minBy = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Accessor function.
42+
*
43+
* @private
44+
* @param {number} value - array element
45+
* @returns {number} accessed value
46+
*/
47+
function accessor( value ) {
48+
return value * 2.0;
49+
}
50+
51+
/**
52+
* Creates a benchmark function.
53+
*
54+
* @private
55+
* @param {PositiveInteger} len - array length
56+
* @returns {Function} benchmark function
57+
*/
58+
function createBenchmark( len ) {
59+
var x = uniform( len, -10, 10, options );
60+
return benchmark;
61+
62+
function benchmark( b ) {
63+
var v;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
v = minBy( x, accessor );
69+
if ( isnan( v ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( v ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':len='+len, f );
104+
}
105+
}
106+
107+
main();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( x, clbk[, thisArg] )
3+
Computes the minimum value of an array via a callback function.
4+
5+
If provided an empty array, the function returns `NaN`.
6+
7+
The callback function is provided three arguments:
8+
9+
- value: current array element.
10+
- index: current array index.
11+
- array: the input array.
12+
13+
The callback function should return a numeric value.
14+
15+
If the callback function does not return any value (or equivalently,
16+
explicitly returns `undefined`), the value is ignored.
17+
18+
Parameters
19+
----------
20+
x: Array<number>|TypedArray
21+
Input array.
22+
23+
clbk: Function
24+
Callback function.
25+
26+
thisArg: any (optional)
27+
Execution context.
28+
29+
Returns
30+
-------
31+
out: number
32+
Minimum value.
33+
34+
Examples
35+
--------
36+
> function accessor( v ) { return v * 2.0; };
37+
> var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ];
38+
> {{alias}}( x, accessor )
39+
-10.0
40+
41+
See Also
42+
--------
43+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
30+
/**
31+
* Returns an accessed value.
32+
*
33+
* @returns accessed value
34+
*/
35+
type Nullary<ThisArg> = ( this: ThisArg ) => number | void;
36+
37+
/**
38+
* Returns an accessed value.
39+
*
40+
* @param value - current array element
41+
* @returns accessed value
42+
*/
43+
type Unary<T, ThisArg> = ( this: ThisArg, value: T ) => number | void;
44+
45+
/**
46+
* Returns an accessed value.
47+
*
48+
* @param value - current array element
49+
* @param index - current array index
50+
* @returns accessed value
51+
*/
52+
type Binary<T, ThisArg> = ( this: ThisArg, value: T, index: number ) => number | void;
53+
54+
/**
55+
* Returns an accessed value.
56+
*
57+
* @param value - current array element
58+
* @param index - current array index
59+
* @param array - input array
60+
* @returns accessed value
61+
*/
62+
type Ternary<T, U, ThisArg> = ( this: ThisArg, value: T, index: number, array: U ) => number | void;
63+
64+
/**
65+
* Returns an accessed value.
66+
*
67+
* @param value - current array element
68+
* @param index - current array index
69+
* @param array - input array
70+
* @returns accessed value
71+
*/
72+
type Callback<T, U, ThisArg> = Nullary<ThisArg> | Unary<T, ThisArg> | Binary<T, ThisArg> | Ternary<T, U, ThisArg>;
73+
74+
/**
75+
* Computes the minimum value of an array via a callback function.
76+
*
77+
* @param x - input array
78+
* @param clbk - callback
79+
* @param thisArg - execution context
80+
* @returns minimum value
81+
*
82+
* @example
83+
* var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
84+
*
85+
* function accessor( v ) {
86+
* return v * 2.0;
87+
* }
88+
*
89+
* var v = minBy( x, accessor );
90+
* // returns -10.0
91+
*/
92+
declare function minBy<T = number, U extends InputArray = InputArray, ThisArg = unknown>( x: U, clbk: Callback<T, U, ThisArg>, thisArg?: ThisParameterType<Callback<T, U, ThisArg>> ): number;
93+
94+
95+
// EXPORTS //
96+
97+
export = minBy;

0 commit comments

Comments
 (0)