Skip to content

Commit 4b0ff12

Browse files
feat: add stats/array/nanrange-by
PR-URL: #7564 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 15fa50d commit 4b0ff12

File tree

10 files changed

+1016
-0
lines changed

10 files changed

+1016
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
# nanrangeBy
22+
23+
> Calculate the [range][range] of an array via a callback function, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [**range**][range] is defined as the difference between the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var nanrangeBy = require( '@stdlib/stats/array/nanrange-by' );
39+
```
40+
41+
#### nanrangeBy( x, clbk\[, thisArg] )
42+
43+
Computes the [range][range] of an array via a callback function, ignoring `NaN` values.
44+
45+
```javascript
46+
function accessor( v ) {
47+
return v * 2.0;
48+
}
49+
50+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ];
51+
52+
var v = nanrangeBy( x, accessor );
53+
// returns 18.0
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **x**: input array.
59+
- **clbk**: callback function.
60+
- **thisArg**: execution context (_optional_).
61+
62+
The invoked callback is provided three arguments:
63+
64+
- **value**: current array element.
65+
- **index**: current array index.
66+
- **array**: input array.
67+
68+
To set the callback execution context, provide a `thisArg`.
69+
70+
```javascript
71+
function accessor( v ) {
72+
this.count += 1;
73+
return v * 2.0;
74+
}
75+
76+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ];
77+
78+
var context = {
79+
'count': 0
80+
};
81+
82+
var v = nanrangeBy( x, accessor, context );
83+
// returns 18.0
84+
85+
var cnt = context.count;
86+
// returns 10
87+
```
88+
89+
</section>
90+
91+
<!-- /.usage -->
92+
93+
<section class="notes">
94+
95+
## Notes
96+
97+
- If provided an empty array, the function returns `NaN`.
98+
- A provided callback function should return a numeric value.
99+
- If a provided callback function returns `NaN`, the value is **ignored**.
100+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
101+
- 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]).
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var uniform = require( '@stdlib/random/base/uniform' );
115+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
116+
var filledarrayBy = require( '@stdlib/array/filled-by' );
117+
var nanrangeBy = require( '@stdlib/stats/array/nanrange-by' );
118+
119+
function rand() {
120+
if ( bernoulli( 0.8 ) < 1 ) {
121+
return NaN;
122+
}
123+
return uniform( -50.0, 50.0 );
124+
}
125+
126+
function accessor( v ) {
127+
return v * 2.0;
128+
}
129+
130+
var x = filledarrayBy( 10, 'float64', rand );
131+
console.log( x );
132+
133+
var v = nanrangeBy( x, accessor );
134+
console.log( v );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
154+
155+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
156+
157+
</section>
158+
159+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var nanrangeBy = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Accessor function.
37+
*
38+
* @private
39+
* @param {number} value - array element
40+
* @returns {number} accessed value
41+
*/
42+
function accessor( value ) {
43+
return value * 2.0;
44+
}
45+
46+
/**
47+
* Returns a random number.
48+
*
49+
* @private
50+
* @returns {number} random number
51+
*/
52+
function rand() {
53+
if ( bernoulli( 0.8 ) < 1 ) {
54+
return NaN;
55+
}
56+
return uniform( -10.0, 10.0 );
57+
}
58+
59+
/**
60+
* Creates a benchmark function.
61+
*
62+
* @private
63+
* @param {PositiveInteger} len - array length
64+
* @returns {Function} benchmark function
65+
*/
66+
function createBenchmark( len ) {
67+
var x = filledarrayBy( len, 'generic', rand );
68+
return benchmark;
69+
70+
function benchmark( b ) {
71+
var v;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
v = nanrangeBy( x, accessor );
77+
if ( isnan( v ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( v ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var f;
103+
var i;
104+
105+
min = 1; // 10^min
106+
max = 6; // 10^max
107+
108+
for ( i = min; i <= max; i++ ) {
109+
len = pow( 10, i );
110+
f = createBenchmark( len );
111+
bench( pkg+':len='+len, f );
112+
}
113+
}
114+
115+
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( x, clbk[, thisArg] )
3+
Computes the range of an array via a callback function, ignoring `NaN`
4+
values.
5+
6+
If provided an empty array, the function returns `NaN`.
7+
8+
The callback function is provided three arguments:
9+
10+
- value: current array element.
11+
- index: current array index.
12+
- array: the input array.
13+
14+
The callback function should return a numeric value.
15+
16+
If the callback function returns `NaN`, the value is ignored.
17+
18+
If the callback function does not return any value (or equivalently,
19+
explicitly returns `undefined`), the value is ignored.
20+
21+
Parameters
22+
----------
23+
x: Array|TypedArray
24+
Input array.
25+
26+
clbk: Function
27+
Callback function.
28+
29+
thisArg: any (optional)
30+
Execution context.
31+
32+
Returns
33+
-------
34+
out: number
35+
Range.
36+
37+
Examples
38+
--------
39+
> function accessor( v ) { return v * 2.0; };
40+
> var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, -1.0, -3.0 ];
41+
> {{alias}}( x, accessor )
42+
18.0
43+
44+
See Also
45+
--------
46+

0 commit comments

Comments
 (0)