Skip to content

Commit 850b149

Browse files
committed
Add iterator utility for concatenating iterator values
1 parent 2bb0074 commit 850b149

File tree

8 files changed

+1172
-0
lines changed

8 files changed

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

0 commit comments

Comments
 (0)