Skip to content

Commit 4114dd6

Browse files
feat: add iter/do-until-each
PR-URL: #1408 Closes: #807 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 9d915c8 commit 4114dd6

File tree

10 files changed

+1369
-0
lines changed

10 files changed

+1369
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
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+
# iterDoUntilEach
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 iterDoUntilEach = require( '@stdlib/iter/do-until-each' );
41+
```
42+
43+
#### iterDoUntilEach( 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. Note that the condition is evaluated **after** executing `fcn`; thus, `fcn` **always** executes at least once.
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 = iterDoUntilEach( 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 > 2 ) {
94+
throw new Error( 'unexpected error' );
95+
}
96+
}
97+
98+
var it = iterDoUntilEach( 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 c = {
132+
'count': 0
133+
};
134+
135+
var it = iterDoUntilEach( array2iterator( [ 1, 2, 3 ] ), predicate, assert, c );
136+
// returns <Object>
137+
138+
var r = it.next().value;
139+
// returns 1
140+
141+
r = it.next().value;
142+
// returns 2
143+
144+
r = it.next().value;
145+
// returns undefined
146+
147+
var count = c.count;
148+
// returns 3
149+
```
150+
151+
</section>
152+
153+
<!-- /.usage -->
154+
155+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="notes">
158+
159+
## Notes
160+
161+
- If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable.
162+
163+
</section>
164+
165+
<!-- /.notes -->
166+
167+
<!-- Package usage examples. -->
168+
169+
<section class="examples">
170+
171+
## Examples
172+
173+
<!-- eslint no-undef: "error" -->
174+
175+
```javascript
176+
var randu = require( '@stdlib/random/iter/randu' );
177+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
178+
var iterDoUntilEach = require( '@stdlib/iter/do-until-each' );
179+
180+
function assert( v ) {
181+
if ( isnan( v ) ) {
182+
throw new Error( 'should not be NaN' );
183+
}
184+
}
185+
186+
function predicate( v ) {
187+
return v <= 0.75;
188+
}
189+
190+
// Create a seeded iterator for generating pseudorandom numbers:
191+
var rand = randu({
192+
'seed': 1234,
193+
'iter': 10
194+
});
195+
196+
// Create an iterator which validates generated numbers:
197+
var it = iterDoUntilEach( rand, predicate, assert );
198+
199+
// Perform manual iteration...
200+
var r;
201+
while ( true ) {
202+
r = it.next();
203+
if ( r.done ) {
204+
break;
205+
}
206+
console.log( r.value );
207+
}
208+
```
209+
210+
</section>
211+
212+
<!-- /.examples -->
213+
214+
<!-- 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. -->
215+
216+
<section class="references">
217+
218+
</section>
219+
220+
<!-- /.references -->
221+
222+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
223+
224+
<section class="related">
225+
226+
</section>
227+
228+
<!-- /.related -->
229+
230+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
231+
232+
<section class="links">
233+
234+
</section>
235+
236+
<!-- /.links -->
Lines changed: 97 additions & 0 deletions
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+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
The condition is evaluated *after* executing the provided function; thus,
8+
`fcn` *always* executes at least once.
9+
10+
When invoked, both input functions are provided two arguments:
11+
12+
- value: iterated value
13+
- index: iteration index (zero-based)
14+
15+
If an environment supports Symbol.iterator, the returned iterator is
16+
iterable.
17+
18+
Parameters
19+
----------
20+
iterator: Object
21+
Input iterator.
22+
23+
predicate: Function
24+
Function which indicates whether to continue iterating.
25+
26+
fcn: Function
27+
Function to invoke for each iterated value.
28+
29+
thisArg: any (optional)
30+
Execution context.
31+
32+
Returns
33+
-------
34+
iterator: Object
35+
Iterator.
36+
37+
iterator.next(): Function
38+
Returns an iterator protocol-compliant object containing the next
39+
iterated value (if one exists) and a boolean flag indicating whether the
40+
iterator is finished.
41+
42+
iterator.return( [value] ): Function
43+
Finishes an iterator and returns a provided value.
44+
45+
Examples
46+
--------
47+
> function predicate( v ) { return v !== v };
48+
> function f( v ) { if ( v !== v ) { throw new Error( 'beep' ); } };
49+
> var it = {{alias}}( {{alias:@stdlib/random/iter/randu}}(), predicate, f );
50+
> var r = it.next().value
51+
<number>
52+
> r = it.next().value
53+
<number>
54+
55+
See Also
56+
--------
57+

0 commit comments

Comments
 (0)