Skip to content

Commit 9deb132

Browse files
committed
Add iterator utility to prepend values to the beginning of an iterator
1 parent 5706e5d commit 9deb132

8 files changed

Lines changed: 1153 additions & 0 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
# iterUnshift
22+
23+
> Create an [iterator][mdn-iterator-protocol] which prepends values to the beginning of a provided [iterator][mdn-iterator-protocol].
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 iterUnshift = require( '@stdlib/iter/unshift' );
41+
```
42+
43+
#### iterUnshift( iterator, ...items )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which prepends values to the **beginning** of a provided [`iterator`][mdn-iterator-protocol].
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var it = iterUnshift( array2iterator( [ 1, 2 ] ), 3, 4 );
51+
// returns <Object>
52+
53+
var v = it.next().value;
54+
// returns 3
55+
56+
v = it.next().value;
57+
// returns 4
58+
59+
v = it.next().value;
60+
// returns 1
61+
62+
v = it.next().value;
63+
// returns 2
64+
65+
var bool = it.next().done;
66+
// returns true
67+
```
68+
69+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
70+
71+
- **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.
72+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
79+
80+
<section class="notes">
81+
82+
## Notes
83+
84+
- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
85+
86+
</section>
87+
88+
<!-- /.notes -->
89+
90+
<!-- Package usage examples. -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var randu = require( '@stdlib/random/iter/randu' );
100+
var iterUnshift = require( '@stdlib/iter/unshift' );
101+
102+
// Create a seeded iterator for generating pseudorandom numbers:
103+
var rand = randu({
104+
'seed': 1234,
105+
'iter': 10
106+
});
107+
108+
// Create an iterator with prepended values:
109+
var it = iterUnshift( rand, 'beep', 'boop' );
110+
111+
// Perform manual iteration...
112+
var v;
113+
while ( true ) {
114+
v = it.next();
115+
if ( v.done ) {
116+
break;
117+
}
118+
console.log( v.value );
119+
}
120+
```
121+
122+
</section>
123+
124+
<!-- /.examples -->
125+
126+
<!-- 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. -->
127+
128+
<section class="references">
129+
130+
</section>
131+
132+
<!-- /.references -->
133+
134+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
135+
136+
<section class="links">
137+
138+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
139+
140+
</section>
141+
142+
<!-- /.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 iterEmpty = require( '@stdlib/iter/empty' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
28+
var pkg = require( './../package.json' ).name;
29+
var iterUnshift = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var iter;
36+
var it;
37+
var i;
38+
39+
it = iterEmpty();
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
iter = iterUnshift( it, i );
44+
if ( typeof iter !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isIteratorLike( iter ) ) {
50+
b.fail( 'should return an iterator protocol-compliant object' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+'::iteration', function benchmark( b ) {
57+
var args;
58+
var iter;
59+
var it;
60+
var z;
61+
var i;
62+
63+
it = randu({
64+
'iter': ( b.iterations > 100 ) ? b.iterations-100 : b.iterations
65+
});
66+
args = [ it ];
67+
for ( i = 0; i < 100; i++ ) {
68+
args.push( i );
69+
}
70+
iter = iterUnshift.apply( null, args );
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
z = iter.next().value;
75+
if ( isnan( z ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( z ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
{{alias}}( iterator, ...items )
3+
Returns an iterator which prepends values to the beginning of a provided
4+
iterator.
5+
6+
If an environment supports Symbol.iterator and a provided iterator is
7+
iterable, the returned iterator is iterable.
8+
9+
Parameters
10+
----------
11+
iterator: Object
12+
Input iterator.
13+
14+
items: ...any
15+
Items to prepend.
16+
17+
Returns
18+
-------
19+
iterator: Object
20+
Iterator.
21+
22+
iterator.next(): Function
23+
Returns an iterator protocol-compliant object containing the next
24+
iterated value (if one exists) and a boolean flag indicating whether the
25+
iterator is finished.
26+
27+
iterator.return( [value] ): Function
28+
Finishes an iterator and returns a provided value.
29+
30+
Examples
31+
--------
32+
> var it1 = {{alias:@stdlib/array/to-iterator}}( [ 1, 2 ] );
33+
> var it2 = {{alias}}( it1, 3, 4 );
34+
> var v = it2.next().value
35+
3
36+
> v = it2.next().value
37+
4
38+
> v = it2.next().value
39+
1
40+
> v = it2.next().value
41+
2
42+
> var bool = it2.next().done
43+
true
44+
45+
See Also
46+
--------
47+
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 iterUnshift = require( './../lib' );
23+
24+
var rand;
25+
var it;
26+
var v;
27+
28+
// Create a seeded iterator for generating pseudorandom numbers:
29+
rand = randu({
30+
'seed': 1234,
31+
'iter': 10
32+
});
33+
34+
// Create an iterator with prepended values:
35+
it = iterUnshift( rand, 'beep', 'boop' );
36+
37+
// Perform manual iteration...
38+
while ( true ) {
39+
v = it.next();
40+
if ( v.done ) {
41+
break;
42+
}
43+
console.log( v.value );
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
/**
22+
* Create an iterator which prepends values to the beginning of a provided iterator.
23+
*
24+
* @module @stdlib/iter/unshift
25+
*
26+
* @example
27+
* var array2iterator = require( '@stdlib/array/to-iterator' );
28+
* var iterUnshift = require( '@stdlib/iter/unshift' );
29+
*
30+
* var iter = iterUnshift( array2iterator( [ 1, 2 ] ), 3, 4 );
31+
*
32+
* var v = iter.next().value;
33+
* // returns 3
34+
*
35+
* v = iter.next().value;
36+
* // returns 4
37+
*
38+
* v = iter.next().value;
39+
* // returns 1
40+
*
41+
* v = iter.next().value;
42+
* // returns 2
43+
*
44+
* var bool = iter.next().done;
45+
* // returns true
46+
*/
47+
48+
// MODULES //
49+
50+
var iterUnshift = require( './main.js' );
51+
52+
53+
// EXPORTS //
54+
55+
module.exports = iterUnshift;

0 commit comments

Comments
 (0)