Skip to content

Commit a0593e0

Browse files
committed
feat: add nanminmax package to handle NaN values
1 parent 55a6c20 commit a0593e0

File tree

10 files changed

+877
-0
lines changed

10 files changed

+877
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
# incrnanminmax
22+
23+
> Compute a minimum and maximum incrementally, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var incrnanminmax = require( '@stdlib/stats/incr/nanminmax' );
31+
```
32+
33+
#### incrnanminmax( \[out] )
34+
35+
Returns an accumulator `function` which incrementally computes a minimum and maximum, ignoring `NaN` values.
36+
37+
```javascript
38+
var accumulator = incrnanminmax();
39+
```
40+
41+
By default, the returned accumulator `function` returns the minimum and maximum as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
42+
43+
44+
#### accumulator( \[x] )
45+
46+
If provided an input value `x`, the accumulator function returns updated minimum and maximum values. If not provided an input value `x`, the accumulator function returns the current minimum and maximum values.
47+
48+
```javascript
49+
var accumulator = incrnanminmax();
50+
51+
var mm = accumulator();
52+
// returns null
53+
54+
mm = accumulator( 2.0 );
55+
// returns [ 2.0, 2.0 ]
56+
57+
mm = accumulator( 1.0 );
58+
// returns [ 1.0, 2.0 ]
59+
60+
mm = accumulator( 3.0 );
61+
// returns [ 1.0, 3.0 ]
62+
63+
mm = accumulator( -7.0 );
64+
// returns [ -7.0, 3.0 ]
65+
66+
mm = accumulator( NaN );
67+
// returns [ -7.0, 3.0 ]
68+
69+
mm = accumulator( -5.0 );
70+
// returns [ -7.0, 3.0 ]
71+
72+
mm = accumulator();
73+
// returns [ -7.0, 3.0 ]
74+
```
75+
76+
</section>
77+
78+
<!-- /.usage -->
79+
80+
<section class="notes">
81+
82+
## Notes
83+
84+
- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
85+
86+
</section>
87+
88+
<!-- /.notes -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var randu = require( '@stdlib/random/base/randu' );
98+
var incrnanminmax = require( './../lib' );
99+
100+
var accumulator;
101+
var v;
102+
var i;
103+
104+
// Initialize an accumulator:
105+
accumulator = incrnanminmax();
106+
107+
// For each simulated datum, update the minimum and maximum...
108+
for ( i = 0; i < 100; i++ ) {
109+
if ( randu() < 0.2 ) {
110+
v = NaN;
111+
} else {
112+
v = randu() * 100.0;
113+
}
114+
}
115+
console.log( accumulator() );
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
* * *
127+
128+
## See Also
129+
130+
- <span class="package-name">[`@stdlib/stats/incr/max`][@stdlib/stats/incr/max]</span><span class="delimiter">: </span><span class="description">compute a maximum value incrementally.</span>
131+
- <span class="package-name">[`@stdlib/stats/incr/min`][@stdlib/stats/incr/min]</span><span class="delimiter">: </span><span class="description">compute a minimum value incrementally.</span>
132+
- <span class="package-name">[`@stdlib/stats/incr/mminmax`][@stdlib/stats/incr/mminmax]</span><span class="delimiter">: </span><span class="description">compute a moving minimum and maximum incrementally.</span>
133+
- <span class="package-name">[`@stdlib/stats/incr/range`][@stdlib/stats/incr/range]</span><span class="delimiter">: </span><span class="description">compute a range incrementally.</span>
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+
<!-- <related-links> -->
144+
145+
[@stdlib/stats/incr/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/max
146+
147+
[@stdlib/stats/incr/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/min
148+
149+
[@stdlib/stats/incr/mminmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mminmax
150+
151+
[@stdlib/stats/incr/range]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/range
152+
153+
<!-- </related-links> -->
154+
155+
</section>
156+
157+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanminmax = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanminmax();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanminmax();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
59+
if ( v.length !== 2 ) {
60+
b.fail( 'should contain two elements' );
61+
}
62+
}
63+
b.toc();
64+
if ( v[ 0 ] !== v[ 0 ] || v[ 1 ] !== v[ 1 ] ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( [out] )
3+
Returns an accumulator function which incrementally computes a minimum and
4+
maximum, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated minimum and
7+
maximum. If not provided a value, the accumulator function returns the
8+
current minimum and maximum.
9+
10+
Parameters
11+
----------
12+
out: Array|TypedArray (optional)
13+
Output array.
14+
15+
Returns
16+
-------
17+
acc: Function
18+
Accumulator function.
19+
20+
Examples
21+
--------
22+
> var accumulator = {{alias}}();
23+
> var mm = accumulator()
24+
null
25+
> mm = accumulator( 2.0 )
26+
[ 2.0, 2.0 ]
27+
> mm = accumulator( -5.0 )
28+
[ -5.0, 2.0 ]
29+
> mm = accumulator( 3.0 )
30+
[ -5.0, 3.0 ]
31+
> mm = accumulator( NaN )
32+
[ -5.0, 3.0 ]
33+
> mm = accumulator( 5.0 )
34+
[ -5.0, 5.0 ]
35+
> mm = accumulator()
36+
[ -5.0, 5.0 ]
37+
38+
See Also
39+
--------
40+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 { ArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* If provided a value, the accumulator function returns updated minimum and maximum values. If not provided a value, the accumulator function returns the current minimum and maximum values.
27+
*
28+
* @param x - input value
29+
* @returns output array or null
30+
*/
31+
type accumulator = ( x?: number ) => ArrayLike<number> | null;
32+
33+
/**
34+
* Returns an accumulator function which incrementally computes minimum and maximum values.
35+
*
36+
* @param out - output array
37+
* @returns accumulator function
38+
*
39+
* @example
40+
* var accumulator = incrminmax();
41+
*
42+
* var mm = accumulator();
43+
* // returns null
44+
*
45+
* mm = accumulator( 2.0 );
46+
* // returns [ 2.0, 2.0 ]
47+
*
48+
* mm = accumulator( -5.0 );
49+
* // returns [ -5.0, 2.0 ]
50+
*
51+
* mm = accumulator( 3.0 );
52+
* // returns [ -5.0, 3.0 ]
53+
*
54+
* mm = accumulator( NaN );
55+
* // returns [ -5.0, 3.0 ]
56+
*
57+
* mm = accumulator( 5.0 );
58+
* // returns [ -5.0, 5.0 ]
59+
*
60+
* mm = accumulator();
61+
* // returns [ -5.0, 5.0 ]
62+
*/
63+
declare function incrnanminmax( out?: ArrayLike<number> ): accumulator;
64+
65+
66+
// EXPORTS //
67+
68+
export = incrnanminmax;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
import incrnanminmax = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanminmax(); // $ExpectType accumulator
27+
const out = [ 0.0, 0.0 ];
28+
incrnanminmax( out ); // $ExpectType accumulator
29+
}
30+
31+
// The compiler throws an error if the function is provided an argument that is not an array-like object of numbers...
32+
{
33+
incrnanminmax( '5' ); // $ExpectError
34+
incrnanminmax( 5 ); // $ExpectError
35+
incrnanminmax( true ); // $ExpectError
36+
incrnanminmax( false ); // $ExpectError
37+
incrnanminmax( null ); // $ExpectError
38+
incrnanminmax( {} ); // $ExpectError
39+
incrnanminmax( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanminmax();
45+
46+
acc(); // $ExpectType ArrayLike<number> | null
47+
acc( 3.14 ); // $ExpectType ArrayLike<number> | null
48+
}
49+
50+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
51+
{
52+
const acc = incrnanminmax();
53+
54+
acc( '5' ); // $ExpectError
55+
acc( true ); // $ExpectError
56+
acc( false ); // $ExpectError
57+
acc( null ); // $ExpectError
58+
acc( [] ); // $ExpectError
59+
acc( {} ); // $ExpectError
60+
acc( ( x: number ): number => x ); // $ExpectError
61+
}

0 commit comments

Comments
 (0)