Skip to content

Commit 091c2e0

Browse files
feat: add stats/incr/nanmin
PR-URL: stdlib-js#5917 Closes: stdlib-js#5551 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 21c4e0f commit 091c2e0

File tree

10 files changed

+728
-0
lines changed

10 files changed

+728
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
# incrnanmin
22+
23+
> Compute a minimum value incrementally, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var incrnanmin = require( '@stdlib/stats/incr/nanmin' );
31+
```
32+
33+
#### incrnanmin()
34+
35+
Returns an accumulator `function` which incrementally computes a minimum value, ignoring `NaN` values.
36+
37+
```javascript
38+
var accumulator = incrnanmin();
39+
```
40+
41+
#### accumulator( \[x] )
42+
43+
If provided an input value `x`, the accumulator function returns an updated minimum value. If not provided an input value `x`, the accumulator function returns the current minimum value.
44+
45+
```javascript
46+
var accumulator = incrnanmin();
47+
48+
var min = accumulator( 2.0 );
49+
// returns 2.0
50+
51+
min = accumulator( 1.0 );
52+
// returns 1.0
53+
54+
min = accumulator( NaN );
55+
// returns 1.0
56+
57+
min = accumulator( 3.0 );
58+
// returns 1.0
59+
60+
min = accumulator();
61+
// returns 1.0
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- 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.
73+
- If all provided values are `NaN`, the accumulator returns `null`.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
```javascript
84+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
85+
var uniform = require( '@stdlib/random/base/uniform' );
86+
var incrnanmin = require( '@stdlib/stats/incr/nanmin' );
87+
88+
var accumulator;
89+
var v;
90+
var i;
91+
92+
// Initialize an accumulator:
93+
accumulator = incrnanmin();
94+
95+
// For each simulated datum, update the min...
96+
for ( i = 0; i < 100; i++ ) {
97+
if ( bernoulli( 0.2 ) ) {
98+
v = NaN;
99+
} else {
100+
v = uniform( 0.0, 100.0 );
101+
}
102+
accumulator( v );
103+
}
104+
console.log( accumulator() );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
<!-- <related-links> -->
124+
125+
<!-- </related-links> -->
126+
127+
</section>
128+
129+
<!-- /.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 incrnanmin = 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 = incrnanmin();
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 = incrnanmin();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{alias}}()
2+
Returns an accumulator function which incrementally computes a minimum
3+
value, ignoring NaN values.
4+
5+
If provided a value, the accumulator function returns an updated minimum
6+
value. If not provided a value, the accumulator function returns the current
7+
minimum value.
8+
9+
If no valid numbers have been provided (only NaN values), the accumulator
10+
function returns null. Unlike incrmin, NaN values are ignored and do not
11+
set the minimum to NaN.
12+
13+
Returns
14+
-------
15+
acc: Function
16+
Accumulator function.
17+
18+
Examples
19+
--------
20+
> var accumulator = {{alias}}();
21+
> var m = accumulator()
22+
null
23+
> m = accumulator( 3.14 )
24+
3.14
25+
> m = accumulator( NaN )
26+
3.14
27+
> m = accumulator( -5.0 )
28+
-5.0
29+
> m = accumulator( 10.1 )
30+
-5.0
31+
> m = accumulator( NaN )
32+
-5.0
33+
> m = accumulator()
34+
-5.0
35+
36+
See Also
37+
--------
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
/**
24+
* If provided a value, returns an updated minimum value; otherwise, returns the current minimum value.
25+
*
26+
* @param x - value
27+
* @returns minimum value
28+
*/
29+
type accumulator = ( x?: number ) => number | null;
30+
31+
/**
32+
* Returns an accumulator function which incrementally computes a minimum value, ignoring `NaN` values.
33+
*
34+
* @returns accumulator function
35+
*
36+
* @example
37+
* var accumulator = incrnanmin();
38+
*
39+
* var m = accumulator();
40+
* // returns null
41+
*
42+
* m = accumulator( 3.14 );
43+
* // returns 3.14
44+
*
45+
* m = accumulator( NaN );
46+
* // returns 3.14
47+
*
48+
* m = accumulator( -5.0 );
49+
* // returns -5.0
50+
*
51+
* m = accumulator( 10.1 );
52+
* // returns -5.0
53+
*
54+
* m = accumulator( NaN );
55+
* // returns -5.0
56+
*
57+
* m = accumulator();
58+
* // returns -5.0
59+
*/
60+
declare function incrnanmin(): accumulator;
61+
62+
63+
// EXPORTS //
64+
65+
export = incrnanmin;
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 incrnanmin = require('./index')
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanmin(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
incrnanmin( '5' ); // $ExpectError
32+
incrnanmin( 5 ); // $ExpectError
33+
incrnanmin( true ); // $ExpectError
34+
incrnanmin( false ); // $ExpectError
35+
incrnanmin( null ); // $ExpectError
36+
incrnanmin( undefined ); // $ExpectError
37+
incrnanmin( [] ); // $ExpectError
38+
incrnanmin( {} ); // $ExpectError
39+
incrnanmin( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanmin();
45+
46+
acc(); // $ExpectType number | null
47+
acc( 3.14 ); // $ExpectType number | null
48+
}
49+
50+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
51+
{
52+
const acc = incrnanmin();
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)