Skip to content

Commit 145835d

Browse files
committed
Add iterator utility to step according to a provided callback function
1 parent 2a471fe commit 145835d

File tree

10 files changed

+2219
-0
lines changed

10 files changed

+2219
-0
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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+
# iterStridedBy
22+
23+
> Create an [iterator][mdn-iterator-protocol] which steps according to a provided callback function.
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 iterStridedBy = require( '@stdlib/iter/strided-by' );
41+
```
42+
43+
#### iterStridedBy( iterator, fcn\[, offset\[, eager]]\[, thisArg] )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which steps according to a provided callback function.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
function stride( v, i ) {
51+
return (i % 10) + 1;
52+
}
53+
54+
var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
55+
var it = iterStridedBy( arr, stride );
56+
// returns <Object>
57+
58+
var r = it.next().value;
59+
// returns 1
60+
61+
r = it.next().value;
62+
// returns 2
63+
64+
r = it.next().value;
65+
// returns 4
66+
67+
// ...
68+
```
69+
70+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
71+
72+
- **next**: function which returns an [iterator][mdn-iterator-protocol] 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.
73+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
74+
75+
The callback function is provided four arguments:
76+
77+
- **value**: iterated value
78+
- **i**: source iteration index (zero-based)
79+
- **n**: iteration index (zero-based)
80+
- **curr**: current stride
81+
82+
To set the callback execution context, provide a `thisArg`.
83+
84+
<!-- eslint-disable no-invalid-this -->
85+
86+
```javascript
87+
var array2iterator = require( '@stdlib/array/to-iterator' );
88+
89+
function stride( v, i ) {
90+
this.count += 1;
91+
return (i % 10) + 1;
92+
}
93+
94+
var ctx = {
95+
'count': 0
96+
};
97+
98+
var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
99+
var it = iterStridedBy( arr, stride, ctx );
100+
// returns <Object>
101+
102+
var v = it.next().value;
103+
// returns 1
104+
105+
v = it.next().value;
106+
// returns 2
107+
108+
v = it.next().value;
109+
// returns 4
110+
111+
v = it.next().value;
112+
// returns 8
113+
114+
var count = ctx.count;
115+
// returns 4
116+
```
117+
118+
To skip the first `N` values of a provided [`iterator`][mdn-iterator-protocol], provide an `offset` argument.
119+
120+
```javascript
121+
var array2iterator = require( '@stdlib/array/to-iterator' );
122+
123+
function stride( v, i ) {
124+
return (i % 10) + 1;
125+
}
126+
127+
var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
128+
var it = iterStridedBy( arr, stride, 1 );
129+
// returns <Object>
130+
131+
var r = it.next().value;
132+
// returns 2
133+
134+
r = it.next().value;
135+
// returns 4
136+
137+
r = it.next().value;
138+
// returns 8
139+
140+
// ...
141+
```
142+
143+
By default, the returned [iterator][mdn-iterator-protocol] defers consuming the first `N` input [`iterator`][mdn-iterator-protocol] values until the first value of the returned [iterator][mdn-iterator-protocol] is consumed. To eagerly advance the input [`iterator`][mdn-iterator-protocol], set the `eager` argument to `true`.
144+
145+
```javascript
146+
var array2iterator = require( '@stdlib/array/to-iterator' );
147+
148+
function stride() {
149+
return 1;
150+
}
151+
152+
var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
153+
var it = iterStridedBy( arr, stride, 4, true );
154+
// returns <Object>
155+
156+
var r = it.next().value;
157+
// returns 5
158+
159+
r = it.next().value;
160+
// returns 6
161+
162+
r = it.next().value;
163+
// returns 7
164+
165+
// ...
166+
```
167+
168+
</section>
169+
170+
<!-- /.usage -->
171+
172+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
173+
174+
<section class="notes">
175+
176+
## Notes
177+
178+
- A callback function **must** return a **positive integer** value.
179+
- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
180+
181+
</section>
182+
183+
<!-- /.notes -->
184+
185+
<!-- Package usage examples. -->
186+
187+
<section class="examples">
188+
189+
## Examples
190+
191+
<!-- eslint no-undef: "error" -->
192+
193+
```javascript
194+
var randu = require( '@stdlib/random/iter/randu' );
195+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
196+
var iterStridedBy = require( '@stdlib/iter/strided-by' );
197+
198+
// Create a seeded iterator for generating pseudorandom numbers:
199+
var rand = randu({
200+
'seed': 1234,
201+
'iter': 10
202+
});
203+
204+
// Create a PRNG for generating pseudorandom integers on the interval [1,10]:
205+
var randi = discreteUniform( 1, 10, {
206+
'seed': 4321
207+
});
208+
209+
// Create an iterator which randomly selects input iterator values:
210+
var it = iterStridedBy( rand, randi );
211+
212+
// Perform manual iteration...
213+
var r;
214+
while ( true ) {
215+
r = it.next();
216+
if ( r.done ) {
217+
break;
218+
}
219+
console.log( r.value );
220+
}
221+
```
222+
223+
</section>
224+
225+
<!-- /.examples -->
226+
227+
<!-- 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. -->
228+
229+
<section class="references">
230+
231+
</section>
232+
233+
<!-- /.references -->
234+
235+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
236+
237+
<section class="links">
238+
239+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
240+
241+
</section>
242+
243+
<!-- /.links -->
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 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 iterStridedBy = 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 = iterStridedBy( rand, stride );
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 stride( v, i ) {
55+
return (i % 10) + 1;
56+
}
57+
});
58+
59+
bench( pkg+'::iteration', function benchmark( b ) {
60+
var rand;
61+
var iter;
62+
var z;
63+
var i;
64+
65+
rand = randu();
66+
iter = iterStridedBy( rand, stride );
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
z = iter.next().value;
71+
if ( isnan( z ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( z ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
82+
function stride( v, i ) {
83+
return (i % 10) + 1;
84+
}
85+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
{{alias}}( iterator, fcn[, offset[, eager]][, thisArg] )
3+
Returns an iterator which steps according to a provided callback function.
4+
5+
When invoked, the input function is provided four arguments:
6+
7+
- value: iterated value
8+
- i: input iteration index (zero-based)
9+
- n: output (strided) iteration index (zero-based)
10+
- curr: current stride
11+
12+
The return value of the input function specifies the next stride.
13+
14+
If an environment supports Symbol.iterator and a provided iterator is
15+
iterable, the returned iterator is iterable.
16+
17+
Parameters
18+
----------
19+
iterator: Object
20+
Input iterator.
21+
22+
fcn: Function
23+
Stride function (i.e., a function which returns the step amount).
24+
25+
offset: integer (optional)
26+
Index of the first iterated value. Default: 0.
27+
28+
eager: boolean (optional)
29+
Boolean indicating whether to eagerly advance the input iterator when
30+
provided a non-zero offset. Default: false.
31+
32+
thisArg: any (optional)
33+
Stride function execution context.
34+
35+
Returns
36+
-------
37+
iterator: Object
38+
Iterator.
39+
40+
iterator.next(): Function
41+
Returns an iterator protocol-compliant object containing the next
42+
iterated value (if one exists) and a boolean flag indicating whether the
43+
iterator is finished.
44+
45+
iterator.return( [value] ): Function
46+
Finishes an iterator and returns a provided value.
47+
48+
Examples
49+
--------
50+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 0, 1, 2, 3, 4, 5, 6 ] );
51+
> function stride( v, i ) { return (i % 10)+1; };
52+
> var it = {{alias}}( arr, stride );
53+
> var r = it.next().value
54+
0
55+
> r = it.next().value
56+
1
57+
> r = it.next().value
58+
3
59+
60+
See Also
61+
--------
62+

0 commit comments

Comments
 (0)