Skip to content

Commit 3fabd41

Browse files
feat: add iter/do-while-each
PR-URL: #1704 Closes: #808 --------- Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 3b6b680 commit 3fabd41

File tree

10 files changed

+1375
-0
lines changed

10 files changed

+1375
-0
lines changed
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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+
# iterDoWhileEach
22+
23+
> Create an iterator which, while a test condition is true, 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 iterDoWhileEach = require( '@stdlib/iter/do-while-each' );
41+
```
42+
43+
#### iterDoWhileEach( 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 false or the iterator has iterated over all values. The condition is evaluated after executing the provided function (fcn).
46+
47+
```javascript
48+
var iterDoWhileEach = require( '@stdlib/iter/do-while-each' );
49+
var array2iterator = require( '@stdlib/array/to-iterator' );
50+
51+
function predicate( v ) {
52+
return v < 3;
53+
}
54+
55+
function assert( v ) {
56+
if ( v !== v ) {
57+
throw new Error( 'should not be NaN' );
58+
}
59+
}
60+
61+
var it = iterDoWhileEach( array2iterator( [ 1, 2, 3, 4 ] ), predicate, assert );
62+
// returns {}
63+
64+
var r = it.next().value;
65+
// returns 1
66+
67+
r = it.next().value;
68+
// returns 2
69+
70+
r = it.next().value;
71+
// returns undefined
72+
73+
// ...
74+
```
75+
76+
The returned iterator protocol-compliant object has the following properties:
77+
78+
- **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.
79+
- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
80+
81+
Both the `predicate` function and the function to invoke for each iterated value are provided two arguments:
82+
83+
- **value**: iterated value
84+
- **index**: iteration index (zero-based)
85+
86+
```javascript
87+
var iterDoWhileEach = require( '@stdlib/iter/do-while-each' );
88+
var array2iterator = require( '@stdlib/array/to-iterator' );
89+
90+
function predicate( v ) {
91+
return v < 3;
92+
}
93+
94+
function assert( v, i ) {
95+
if ( v !== v ) {
96+
throw new Error( 'should not be NaN' );
97+
}
98+
}
99+
100+
var it = iterDoWhileEach( array2iterator( [ 1, 2, 3, 4 ] ), predicate, assert );
101+
// returns <Object>
102+
103+
var r = it.next().value;
104+
// returns 1
105+
106+
r = it.next().value;
107+
// returns 2
108+
109+
r = it.next().value;
110+
// returns undefined
111+
// ...
112+
```
113+
114+
To set the execution context for `fcn`, provide a `thisArg`.
115+
116+
<!-- eslint-disable no-invalid-this -->
117+
118+
```javascript
119+
var iterDoWhileEach = require( '@stdlib/iter/do-while-each' );
120+
var array2iterator = require( '@stdlib/array/to-iterator' );
121+
122+
function assert( v ) {
123+
this.count += 1;
124+
if ( v !== v ) {
125+
throw new Error( 'should not be NaN' );
126+
}
127+
}
128+
129+
function predicate( v ) {
130+
return v < 3;
131+
}
132+
133+
var ctx = {
134+
'count': 0
135+
};
136+
137+
var iterator = array2iterator( [ 1, 2, 3 ] );
138+
var it = iterDoWhileEach( iterator, predicate, assert, ctx );
139+
// returns <Object>
140+
141+
var r = it.next().value;
142+
// returns 1
143+
144+
r = it.next().value;
145+
// returns 2
146+
147+
r = it.next().value;
148+
// returns undefined
149+
150+
var count = ctx.count;
151+
// returns 3
152+
```
153+
154+
</section>
155+
156+
<!-- /.usage -->
157+
158+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="notes">
161+
162+
## Notes
163+
164+
- If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable.
165+
166+
</section>
167+
168+
<!-- /.notes -->
169+
170+
<!-- Package usage examples. -->
171+
172+
<section class="examples">
173+
174+
## Examples
175+
176+
<!-- eslint no-undef: "error" -->
177+
178+
```javascript
179+
var randu = require( '@stdlib/random/iter/randu' );
180+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
181+
var iterDoWhileEach = require( '@stdlib/iter/do-while-each' );
182+
183+
function assert( v ) {
184+
if ( isnan( v ) ) {
185+
throw new Error( 'should not be NaN' );
186+
}
187+
}
188+
189+
function predicate( v ) {
190+
return v <= 0.75;
191+
}
192+
193+
// Create a seeded iterator for generating pseudorandom numbers:
194+
var rand = randu({
195+
'seed': 1234,
196+
'iter': 10
197+
});
198+
199+
// Create an iterator which validates generated numbers:
200+
var it = iterDoWhileEach( rand, predicate, assert );
201+
202+
// Perform manual iteration...
203+
var r;
204+
while ( true ) {
205+
r = it.next();
206+
if ( r.done ) {
207+
break;
208+
}
209+
console.log( r.value );
210+
}
211+
```
212+
213+
</section>
214+
215+
<!-- /.examples -->
216+
217+
<!-- 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. -->
218+
219+
<section class="references">
220+
221+
</section>
222+
223+
<!-- /.references -->
224+
225+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
226+
227+
<section class="related">
228+
229+
</section>
230+
231+
<!-- /.related -->
232+
233+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
234+
235+
<section class="links">
236+
237+
</section>
238+
239+
<!-- /.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 iterDoWhileEach = 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 = iterDoWhileEach( 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 = iterDoWhileEach( 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 false
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)