Skip to content

Commit 73ab86a

Browse files
JawHawkstdlib-bot
andauthored
feat: add iter/until-each
PR-URL: #1389 Closes: #809 Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent fa2359f commit 73ab86a

File tree

10 files changed

+1363
-0
lines changed

10 files changed

+1363
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# iterUntilEach
22+
23+
> Create an iterator which, while a test condition is false, invokes a function for each iterated value before returning the iterated value.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var iterUntilEach = require( '@stdlib/iter/until-each' );
41+
```
42+
43+
#### iterUntilEach( iterator, predicate, fcn\[, thisArg] )
44+
45+
Returns an iterator which invokes a function for each iterated value **before** returning the iterated value until either a `predicate` function returns `true` or the iterator has iterated over all values.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
function predicate( v ) {
51+
return v > 2;
52+
}
53+
54+
function assert( v ) {
55+
if ( v !== v ) {
56+
throw new Error( 'should not be NaN' );
57+
}
58+
}
59+
60+
var it = iterUntilEach( array2iterator( [ 1, 2, 3, 4 ] ), predicate, assert );
61+
// returns {}
62+
63+
var r = it.next().value;
64+
// returns 1
65+
66+
r = it.next().value;
67+
// returns 2
68+
69+
r = it.next().value;
70+
// undefined
71+
72+
// ...
73+
```
74+
75+
The returned iterator protocol-compliant object has the following properties:
76+
77+
- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a boolean value indicating whether the iterator is finished.
78+
- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
79+
80+
Both the `predicate` function and the function to invoke for each iterated value are provided two arguments:
81+
82+
- **value**: iterated value
83+
- **index**: iteration index (zero-based)
84+
85+
```javascript
86+
var array2iterator = require( '@stdlib/array/to-iterator' );
87+
88+
function predicate( v ) {
89+
return v > 2;
90+
}
91+
92+
function assert( v, i ) {
93+
if ( i > 1 ) {
94+
throw new Error( 'unexpected error' );
95+
}
96+
}
97+
98+
var it = iterUntilEach( array2iterator( [ 1, 2, 3, 4 ] ), predicate, assert );
99+
// returns <Object>
100+
101+
var r = it.next().value;
102+
// returns 1
103+
104+
r = it.next().value;
105+
// returns 2
106+
107+
r = it.next().value;
108+
// undefined
109+
110+
// ...
111+
```
112+
113+
To set the execution context for `fcn`, provide a `thisArg`.
114+
115+
<!-- eslint-disable no-invalid-this -->
116+
117+
```javascript
118+
var array2iterator = require( '@stdlib/array/to-iterator' );
119+
120+
function assert( v ) {
121+
this.count += 1;
122+
if ( v !== v ) {
123+
throw new Error( 'should not be NaN' );
124+
}
125+
}
126+
127+
function predicate( v ) {
128+
return v > 2;
129+
}
130+
131+
var ctx = {
132+
'count': 0
133+
};
134+
135+
var it = iterUntilEach( array2iterator( [ 1, 2, 3 ] ), predicate, assert, ctx );
136+
// returns <Object>
137+
138+
var r = it.next().value;
139+
// returns 1
140+
141+
r = it.next().value;
142+
// returns 2
143+
144+
var count = ctx.count;
145+
// returns 2
146+
```
147+
148+
</section>
149+
150+
<!-- /.usage -->
151+
152+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="notes">
155+
156+
## Notes
157+
158+
- If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable.
159+
160+
</section>
161+
162+
<!-- /.notes -->
163+
164+
<!-- Package usage examples. -->
165+
166+
<section class="examples">
167+
168+
## Examples
169+
170+
<!-- eslint no-undef: "error" -->
171+
172+
```javascript
173+
var randu = require( '@stdlib/random/iter/randu' );
174+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
175+
var iterUntilEach = require( '@stdlib/iter/until-each' );
176+
177+
function assert( v ) {
178+
if ( isnan( v ) ) {
179+
throw new Error( 'should not be NaN' );
180+
}
181+
}
182+
183+
function predicate( v ) {
184+
return v <= 0.75;
185+
}
186+
187+
// Create a seeded iterator for generating pseudorandom numbers:
188+
var rand = randu({
189+
'seed': 1234,
190+
'iter': 10
191+
});
192+
193+
// Create an iterator which validates generated numbers:
194+
var it = iterUntilEach( rand, predicate, assert );
195+
196+
// Perform manual iteration...
197+
var r;
198+
while ( true ) {
199+
r = it.next();
200+
if ( r.done ) {
201+
break;
202+
}
203+
console.log( r.value );
204+
}
205+
```
206+
207+
</section>
208+
209+
<!-- /.examples -->
210+
211+
<!-- 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. -->
212+
213+
<section class="references">
214+
215+
</section>
216+
217+
<!-- /.references -->
218+
219+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
220+
221+
<section class="related">
222+
223+
</section>
224+
225+
<!-- /.related -->
226+
227+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
228+
229+
<section class="links">
230+
231+
</section>
232+
233+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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/iter/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
27+
var pkg = require( './../package.json' ).name;
28+
var iterator = 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 = iterator( rand, predicate, fcn );
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+
function fcn( v ) {
55+
if ( isnan( v ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
60+
function predicate( v ) {
61+
return ( v < 0.5 );
62+
}
63+
});
64+
65+
bench( pkg+'::iteration', function benchmark( b ) {
66+
var rand;
67+
var iter;
68+
var z;
69+
var i;
70+
71+
rand = randu();
72+
iter = iterator( rand, predicate, fcn );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
z = iter.next().value;
77+
if ( isnan( z ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( z ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
88+
function fcn( v ) {
89+
if ( isnan( v ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
}
93+
94+
function predicate( v ) {
95+
return ( v < 0.5 );
96+
}
97+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
{{alias}}( iterator, predicate, fcn[, thisArg] )
3+
Returns an iterator which invokes a function for each iterated value before
4+
returning the iterated value until either a predicate function returns true
5+
or the iterator has iterated over all values.
6+
7+
When invoked, both input functions are provided two arguments:
8+
9+
- value: iterated value
10+
- index: iteration index (zero-based)
11+
12+
If an environment supports Symbol.iterator, the returned iterator is
13+
iterable.
14+
15+
Parameters
16+
----------
17+
iterator: Object
18+
Input iterator.
19+
20+
predicate: Function
21+
Function which indicates whether to continue iterating.
22+
23+
fcn: Function
24+
Function to invoke for each iterated value.
25+
26+
thisArg: any (optional)
27+
Execution context.
28+
29+
Returns
30+
-------
31+
iterator: Object
32+
Iterator.
33+
34+
iterator.next(): Function
35+
Returns an iterator protocol-compliant object containing the next
36+
iterated value (if one exists) and a boolean flag indicating whether the
37+
iterator is finished.
38+
39+
iterator.return( [value] ): Function
40+
Finishes an iterator and returns a provided value.
41+
42+
Examples
43+
--------
44+
> function predicate( v ) { return v !== v };
45+
> function f( v ) { if ( v !== v ) { throw new Error( 'beep' ); } };
46+
> var it = {{alias}}( {{alias:@stdlib/random/iter/randu}}(), predicate, f );
47+
> var r = it.next().value
48+
<number>
49+
> r = it.next().value
50+
<number>
51+
52+
See Also
53+
--------
54+

0 commit comments

Comments
 (0)