Skip to content

Commit 2a7f144

Browse files
committed
Add iterator utility to "fill" a provided iterator
1 parent 421ffc7 commit 2a7f144

File tree

8 files changed

+1526
-0
lines changed

8 files changed

+1526
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
# iterFill
22+
23+
> Create an [iterator][mdn-iterator-protocol] which replaces all values from a provided [iterator][mdn-iterator-protocol] from a start index to an end index with a static 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 iterFill = require( '@stdlib/iter/fill' );
41+
```
42+
43+
#### iterFill( iterator, value\[, begin\[, end]] )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which replaces all values from a provided [`iterator`][mdn-iterator-protocol] from a `begin` index to an `end` index with a static `value`.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var it = iterFill( array2iterator( [ 1, 2, 3, 4 ] ), 3.14 );
51+
// returns <Object>
52+
53+
var v = it.next().value;
54+
// returns 3.14
55+
56+
v = it.next().value;
57+
// returns 3.14
58+
59+
v = it.next().value;
60+
// returns 3.14
61+
62+
// ...
63+
```
64+
65+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
66+
67+
- **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][mdn-iterator-protocol] is finished.
68+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
69+
70+
By default, the returned [iterator][mdn-iterator-protocol] replaces/fills a provided [iterator's][mdn-iterator-protocol] first iterated value through an [iterator's][mdn-iterator-protocol] last iterated value. To specify an alternative start iteration index at which to begin filling (zero-based and **inclusive**), provide a `begin` argument.
71+
72+
```javascript
73+
var array2iterator = require( '@stdlib/array/to-iterator' );
74+
75+
var it = iterFill( array2iterator( [ 1, 2, 3, 4 ] ), 3.14, 2 );
76+
// returns <Object>
77+
78+
var v = it.next().value;
79+
// returns 1
80+
81+
v = it.next().value;
82+
// returns 2
83+
84+
v = it.next().value;
85+
// returns 3.14
86+
87+
v = it.next().value;
88+
// returns 3.14
89+
90+
var bool = it.next().done;
91+
// returns true
92+
```
93+
94+
By default, the returned [iterator][mdn-iterator-protocol] continues filling until it replaces all of a provided [iterator's][mdn-iterator-protocol] iterated values. To specify an end iteration index at which to stop filling (zero-based and **non-inclusive**), provide an `end` argument.
95+
96+
```javascript
97+
var array2iterator = require( '@stdlib/array/to-iterator' );
98+
99+
var it = iterFill( array2iterator( [ 1, 2, 3, 4 ] ), 3.14, 1, 3 );
100+
// returns <Object>
101+
102+
var v = it.next().value;
103+
// returns 1
104+
105+
v = it.next().value;
106+
// returns 3.14
107+
108+
v = it.next().value;
109+
// returns 3.14
110+
111+
v = it.next().value;
112+
// returns 4
113+
114+
var bool = it.next().done;
115+
// returns true
116+
```
117+
118+
If `end` is greater than or equal to `begin`, the returned [iterator][mdn-iterator-protocol] does not replace any iterated values.
119+
120+
```javascript
121+
var array2iterator = require( '@stdlib/array/to-iterator' );
122+
123+
var it = iterFill( array2iterator( [ 1, 2, 3, 4 ] ), 3.14, 3, 1 );
124+
// returns <Object>
125+
126+
var v = it.next().value;
127+
// returns 1
128+
129+
v = it.next().value;
130+
// returns 2
131+
132+
v = it.next().value;
133+
// returns 3
134+
135+
v = it.next().value;
136+
// returns 4
137+
138+
var bool = it.next().done;
139+
// returns true
140+
```
141+
142+
</section>
143+
144+
<!-- /.usage -->
145+
146+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="notes">
149+
150+
## Notes
151+
152+
- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
153+
154+
</section>
155+
156+
<!-- /.notes -->
157+
158+
<!-- Package usage examples. -->
159+
160+
<section class="examples">
161+
162+
## Examples
163+
164+
<!-- eslint no-undef: "error" -->
165+
166+
```javascript
167+
var randu = require( '@stdlib/random/iter/randu' );
168+
var iterFill = require( '@stdlib/iter/fill' );
169+
170+
var rand;
171+
var it;
172+
var r;
173+
174+
// Create a seeded iterator for generating pseudorandom numbers:
175+
rand = randu({
176+
'seed': 1234,
177+
'iter': 100
178+
});
179+
180+
// Create an iterator which replaces a subsequence of 10 generated numbers:
181+
it = iterFill( rand, 3.14, 10, 20 );
182+
183+
// Perform manual iteration...
184+
while ( true ) {
185+
r = it.next();
186+
if ( r.done ) {
187+
break;
188+
}
189+
console.log( r.value );
190+
}
191+
```
192+
193+
</section>
194+
195+
<!-- /.examples -->
196+
197+
<!-- 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. -->
198+
199+
<section class="references">
200+
201+
</section>
202+
203+
<!-- /.references -->
204+
205+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
206+
207+
<section class="links">
208+
209+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
210+
211+
</section>
212+
213+
<!-- /.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 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 iterFill = 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 = iterFill( rand, 3.14, 5, 10 );
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 rand;
57+
var iter;
58+
var z;
59+
var i;
60+
61+
rand = randu();
62+
iter = iterFill( rand, 3.14, 0, b.iterations+1 );
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
z = iter.next().value;
67+
if ( isnan( z ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( z ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
{{alias}}( iterator, value[, begin[, end]] )
3+
Returns an iterator which replaces all values from a provided iterator from
4+
a start index to an end index with a static value.
5+
6+
If `end` exceeds the length of the provided iterator, the returned iterator
7+
replaces the subsequence of iterated values starting from `begin` until the
8+
last iterated value of the provided iterator.
9+
10+
If an environment supports Symbol.iterator, the returned iterator is
11+
iterable.
12+
13+
Parameters
14+
----------
15+
iterator: Object
16+
Input iterator.
17+
18+
value: any
19+
Static value.
20+
21+
begin: integer (optional)
22+
Start iteration index (zero-based and inclusive). Default: 0.
23+
24+
end: integer (optional)
25+
End iteration index (zero-based and non-inclusive).
26+
27+
Returns
28+
-------
29+
iterator: Object
30+
Iterator.
31+
32+
iterator.next(): Function
33+
Returns an iterator protocol-compliant object containing the next
34+
iterated value (if one exists) and a boolean flag indicating whether the
35+
iterator is finished.
36+
37+
iterator.return( [value] ): Function
38+
Finishes an iterator and returns a provided value.
39+
40+
Examples
41+
--------
42+
> var it = {{alias}}( {{alias:@stdlib/random/iter/randu}}(), 3.14, 0, 2 );
43+
> var r = it.next().value
44+
3.14
45+
> r = it.next().value
46+
3.14
47+
> r = it.next().value
48+
<number>
49+
50+
See Also
51+
--------
52+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 randu = require( '@stdlib/random/iter/randu' );
22+
var iterFill = require( './../lib' );
23+
24+
var rand;
25+
var it;
26+
var r;
27+
28+
// Create a seeded iterator for generating pseudorandom numbers:
29+
rand = randu({
30+
'seed': 1234,
31+
'iter': 100
32+
});
33+
34+
// Create an iterator which replaces a subsequence of 10 generated numbers:
35+
it = iterFill( rand, 3.14, 10, 20 );
36+
37+
// Perform manual iteration...
38+
while ( true ) {
39+
r = it.next();
40+
if ( r.done ) {
41+
break;
42+
}
43+
console.log( r.value );
44+
}

0 commit comments

Comments
 (0)