Skip to content

Commit ee2a82e

Browse files
committed
Add iterator utility to iteratively compute a cumulative maximum value
1 parent a9d52b2 commit ee2a82e

File tree

8 files changed

+882
-0
lines changed

8 files changed

+882
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2019 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+
# itercumax
22+
23+
> Create an [iterator][mdn-iterator-protocol] which iteratively computes a cumulative maximum value.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<!-- Package usage documentation. -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var itercumax = require( '@stdlib/stats/iter/cumax' );
39+
```
40+
41+
#### itercumax( iterator )
42+
43+
Returns an [iterator][mdn-iterator-protocol] which iteratively computes a cumulative maximum value.
44+
45+
```javascript
46+
var array2iterator = require( '@stdlib/array/to-iterator' );
47+
48+
var arr = array2iterator( [ 2.0, 1.0, 3.0, -7.0, -5.0 ] );
49+
var it = itercumax( arr );
50+
51+
var m = it.next().value;
52+
// returns 2.0
53+
54+
m = it.next().value;
55+
// returns 2.0
56+
57+
m = it.next().value;
58+
// returns 3.0
59+
60+
m = it.next().value;
61+
// returns 3.0
62+
63+
m = it.next().value;
64+
// returns 3.0
65+
```
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
72+
73+
<section class="notes">
74+
75+
## Notes
76+
77+
- If an iterated value is non-numeric (including `NaN`), the function returns `NaN` for **all** future iterations. If non-numeric iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles non-numeric values accordingly.
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<!-- Package usage examples. -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var runif = require( '@stdlib/random/iter/uniform' );
93+
var itercumax = require( '@stdlib/stats/iter/cumax' );
94+
95+
// Create an iterator for generating uniformly distributed pseudorandom numbers:
96+
var rand = runif( -10.0, 10.0, {
97+
'seed': 1234,
98+
'iter': 100
99+
});
100+
101+
// Create an iterator for iteratively computing a cumulative maximum value:
102+
var it = itercumax( rand );
103+
104+
// Perform manual iteration...
105+
var v;
106+
while ( true ) {
107+
v = it.next();
108+
if ( typeof v.value === 'number' ) {
109+
console.log( 'max: %d', v.value );
110+
}
111+
if ( v.done ) {
112+
break;
113+
}
114+
}
115+
```
116+
117+
</section>
118+
119+
<!-- /.examples -->
120+
121+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
122+
123+
<section class="references">
124+
125+
</section>
126+
127+
<!-- /.references -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
134+
135+
</section>
136+
137+
<!-- /.links -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
26+
var randu = require( '@stdlib/random/iter/randu' );
27+
var pkg = require( './../package.json' ).name;
28+
var itercumax = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var rand;
35+
var iter;
36+
var i;
37+
38+
rand = randu();
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = itercumax( rand );
43+
if ( typeof iter !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isIteratorLike( iter ) ) {
49+
b.fail( 'should return an iterator protocol-compliant object' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::iteration', function benchmark( b ) {
56+
var iter;
57+
var rand;
58+
var v;
59+
var i;
60+
61+
rand = randu();
62+
iter = itercumax( rand );
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
v = iter.next().value;
67+
if ( isnan( v ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( v ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( iterator )
3+
Returns an iterator which iteratively computes a cumulative maximum value.
4+
5+
If an environment supports Symbol.iterator, the returned iterator is
6+
iterable.
7+
8+
Parameters
9+
----------
10+
iterator: Object
11+
Input iterator.
12+
13+
Returns
14+
-------
15+
iterator: Object
16+
Iterator.
17+
18+
iterator.next(): Function
19+
Returns an iterator protocol-compliant object containing the next
20+
iterated value (if one exists) and a boolean flag indicating whether the
21+
iterator is finished.
22+
23+
iterator.return( [value] ): Function
24+
Finishes an iterator and returns a provided value.
25+
26+
Examples
27+
--------
28+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 2.0, -5.0, 3.0, 5.0 ] );
29+
> var it = {{alias}}( arr, 3 );
30+
> var m = it.next().value
31+
2.0
32+
> m = it.next().value
33+
2.0
34+
> m = it.next().value
35+
3.0
36+
> m = it.next().value
37+
5.0
38+
39+
See Also
40+
--------
41+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
var runif = require( '@stdlib/random/iter/uniform' );
22+
var itercumax = require( './../lib' );
23+
24+
// Create an iterator for generating uniformly distributed pseudorandom numbers:
25+
var rand = runif( -10.0, 10.0, {
26+
'seed': 1234,
27+
'iter': 100
28+
});
29+
30+
// Create an iterator for iteratively computing a cumulative maximum value:
31+
var it = itercumax( rand );
32+
33+
// Perform manual iteration...
34+
var v;
35+
while ( true ) {
36+
v = it.next();
37+
if ( typeof v.value === 'number' ) {
38+
console.log( 'max: %d', v.value );
39+
}
40+
if ( v.done ) {
41+
break;
42+
}
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
/**
22+
* Create an iterator which iteratively computes a cumulative maximum value.
23+
*
24+
* @module @stdlib/stats/iter/cumax
25+
*
26+
* @example
27+
* var runif = require( '@stdlib/random/iter/uniform' );
28+
* var itercumax = require( '@stdlib/stats/iter/cumax' );
29+
*
30+
* var rand = runif( -10.0, 10.0, {
31+
* 'iter': 100
32+
* });
33+
*
34+
* var it = itercumax( rand );
35+
*
36+
* var v = it.next().value;
37+
* // returns <number>
38+
*
39+
* v = it.next().value;
40+
* // returns <number>
41+
*
42+
* v = it.next().value;
43+
* // returns <number>
44+
*
45+
* // ...
46+
*/
47+
48+
// MODULES //
49+
50+
var iterator = require( './main.js' );
51+
52+
53+
// EXPORTS //
54+
55+
module.exports = iterator;

0 commit comments

Comments
 (0)