Skip to content

Commit fc1e303

Browse files
committed
feat: add stats/array/max-by
1 parent 63b7891 commit fc1e303

File tree

10 files changed

+990
-0
lines changed

10 files changed

+990
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
# maxBy
22+
23+
> Calculate the maximum 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 maxBy = require( '@stdlib/stats/array/max-by' );
37+
```
38+
39+
#### maxBy( x, clbk\[, thisArg] )
40+
41+
Computes the maximum 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 = maxBy( x, accessor );
51+
// returns 8.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 four arguments:
61+
62+
- **value**: array element.
63+
- **aidx**: array index.
64+
- **sidx**: strided index (`offset + aidx*stride`).
65+
- **array**: input array/collection.
66+
67+
To set the callback execution context, provide a `thisArg`.
68+
69+
```javascript
70+
function accessor( v ) {
71+
this.count += 1;
72+
return v * 2.0;
73+
}
74+
75+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
76+
77+
var context = {
78+
'count': 0
79+
};
80+
81+
var v = maxBy( x, accessor, context );
82+
// returns 8.0
83+
84+
var cnt = context.count;
85+
// returns 8
86+
```
87+
88+
</section>
89+
90+
<!-- /.usage -->
91+
92+
<section class="notes">
93+
94+
## Notes
95+
96+
- If provided an empty array, the function returns `NaN`.
97+
- A provided callback function should return a numeric value.
98+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
99+
- 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]).
100+
- When possible, prefer using [`dmax`][@stdlib/stats/array/dmax], [`smax`][@stdlib/stats/array/smax], and/or [`max`][@stdlib/stats/array/max], as, depending on the environment, these interfaces are likely to be significantly more performant.
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
114+
var maxBy = require( '@stdlib/stats/array/max-by' );
115+
116+
function accessor( v ) {
117+
return v * 2.0;
118+
}
119+
120+
var x = discreteUniform( 10, -50, 50, {
121+
'dtype': 'float64'
122+
});
123+
console.log( x );
124+
125+
var v = maxBy( x, accessor );
126+
console.log( v );
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
134+
135+
<section class="related">
136+
137+
</section>
138+
139+
<!-- /.related -->
140+
141+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="links">
144+
145+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
146+
147+
[@stdlib/stats/array/dmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/array/dmax
148+
149+
[@stdlib/stats/array/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/array/max
150+
151+
[@stdlib/stats/array/smax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/array/smax
152+
153+
</section>
154+
155+
<!-- /.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 maxBy = 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 = maxBy( 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( x )
3+
Computes the maximum 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: array element.
10+
- aidx: array index.
11+
- sidx: strided index (offset + aidx*stride).
12+
- array: the input array.
13+
14+
The callback function should return a numeric value.
15+
16+
If the callback function does not return any value (or equivalently,
17+
explicitly returns `undefined`), the value is ignored.
18+
19+
Parameters
20+
----------
21+
x: Array<number>|TypedArray
22+
Input array.
23+
24+
clbk: Function
25+
Callback function.
26+
27+
thisArg: any (optional)
28+
Execution context.
29+
30+
Returns
31+
-------
32+
out: number
33+
Maximum value.
34+
35+
Examples
36+
--------
37+
> function accessor( v ) { return v * 2.0; };
38+
> var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ];
39+
> {{alias}}( x, accessor )
40+
8.0
41+
42+
See Also
43+
--------
44+

0 commit comments

Comments
 (0)