Skip to content

Commit fb51084

Browse files
committed
Add iterator utility to return the union of two or more iterators
1 parent b3af423 commit fb51084

File tree

8 files changed

+1378
-0
lines changed

8 files changed

+1378
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
# iterUnion
22+
23+
> Create an [iterator][mdn-iterator-protocol] which returns the union 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 iterUnion = require( '@stdlib/iter/union' );
41+
```
42+
43+
#### iterUnion( iter0, ...iterator )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which returns the union of two or more [iterators][mdn-iterator-protocol].
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var it1 = array2iterator( [ 2, 1, 1, 2, 4 ] );
51+
var it2 = array2iterator( [ 3, 4, 3 ] );
52+
53+
var it = iterUnion( it1, it2 );
54+
// returns <Object>
55+
56+
var v = it.next().value;
57+
// returns 2
58+
59+
v = it.next().value;
60+
// returns 1
61+
62+
v = it.next().value;
63+
// returns 4
64+
65+
v = it.next().value;
66+
// returns 3
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+
- Value "uniqueness" is determined according to **strict equality**.
88+
- A returned [iterator][mdn-iterator-protocol] internally buffers unique values and, thus, has `O(N)` memory requirements, where `N` is the total number of [iterator][mdn-iterator-protocol] source values.
89+
- If an environment supports `Symbol.iterator` **and** all provided [iterators][mdn-iterator-protocol] are iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<!-- Package usage examples. -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var discreteUniform = require( '@stdlib/random/iter/discrete-uniform' );
105+
var iterUnion = require( '@stdlib/iter/union' );
106+
107+
// Create seeded iterators which can generate 1000 pseudorandom numbers:
108+
var rand1 = discreteUniform( 1, 10, {
109+
'seed': 1234,
110+
'iter': 1000
111+
});
112+
var rand2 = discreteUniform( 6, 15, {
113+
'seed': 1234,
114+
'iter': 1000
115+
});
116+
117+
// Create an iterator which returns the union of the seeded iterators:
118+
var it = iterUnion( rand1, rand2 );
119+
120+
// Perform manual iteration...
121+
var v;
122+
while ( true ) {
123+
v = it.next();
124+
if ( v.done ) {
125+
break;
126+
}
127+
console.log( v.value );
128+
}
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- 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. -->
136+
137+
<section class="references">
138+
139+
</section>
140+
141+
<!-- /.references -->
142+
143+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
144+
145+
<section class="links">
146+
147+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
148+
149+
</section>
150+
151+
<!-- /.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 discreteUniform = require( '@stdlib/random/iter/discrete-uniform' );
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 iterUnion = 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 = discreteUniform( 0, 100 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = iterUnion( 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 = discreteUniform( 1, 1000, {
62+
'iter': b.iterations
63+
});
64+
iter = iterUnion( 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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
{{alias}}( iter0, ...iterator )
3+
Returns an iterator which returns the union of two or more iterators.
4+
5+
Value "uniqueness" is determined according to strict equality.
6+
7+
A returned iterator internally buffers unique values and, thus, has O(N)
8+
memory requirements, where `N` is the total number of source iterator
9+
values.
10+
11+
If an environment supports Symbol.iterator and all provided iterators are
12+
iterable, the returned iterator is iterable.
13+
14+
Parameters
15+
----------
16+
iter0: Object
17+
Input iterator.
18+
19+
...iterator: Object
20+
Input iterators.
21+
22+
Returns
23+
-------
24+
iterator: Object
25+
Iterator.
26+
27+
iterator.next(): Function
28+
Returns an iterator protocol-compliant object containing the next
29+
iterated value (if one exists) and a boolean flag indicating whether the
30+
iterator is finished.
31+
32+
iterator.return( [value] ): Function
33+
Finishes an iterator and returns a provided value.
34+
35+
Examples
36+
--------
37+
> var it1 = {{alias:@stdlib/array/to-iterator}}( [ 1, 2, 1, 2, 4 ] );
38+
> var it2 = {{alias:@stdlib/array/to-iterator}}( [ 1, 2, 5, 2, 3 ] );
39+
> var it = {{alias}}( it1, it2 );
40+
> var v = it.next().value
41+
1
42+
> v = it.next().value
43+
2
44+
> v = it.next().value
45+
4
46+
> v = it.next().value
47+
5
48+
> v = it.next().value
49+
3
50+
> var bool = it.next().done
51+
true
52+
53+
See Also
54+
--------
55+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 discreteUniform = require( '@stdlib/random/iter/discrete-uniform' );
22+
var iterUnion = require( './../lib' );
23+
24+
// Create seeded iterators which can generate 1000 pseudorandom numbers:
25+
var rand1 = discreteUniform( 1, 10, {
26+
'seed': 1234,
27+
'iter': 1000
28+
});
29+
var rand2 = discreteUniform( 6, 15, {
30+
'seed': 1234,
31+
'iter': 1000
32+
});
33+
34+
// Create an iterator which returns the union of the seeded iterators:
35+
var it = iterUnion( rand1, rand2 );
36+
37+
// Perform manual iteration...
38+
var v;
39+
while ( true ) {
40+
v = it.next();
41+
if ( v.done ) {
42+
break;
43+
}
44+
console.log( v.value );
45+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 returns the union of two or more iterators.
23+
*
24+
* @module @stdlib/iter/union
25+
*
26+
* @example
27+
* var array2iterator = require( '@stdlib/array/to-iterator' );
28+
* var iterUnion = require( '@stdlib/iter/union' );
29+
*
30+
* var it1 = array2iterator( [ 1, 2, 1, 2, 4 ] );
31+
* var it2 = array2iterator( [ 1, 2, 5, 2, 3 ] );
32+
*
33+
* var iter = iterUnion( 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 4
43+
*
44+
* v = iter.next().value;
45+
* // returns 5
46+
*
47+
* v = iter.next().value;
48+
* // returns 3
49+
*
50+
* var bool = iter.next().done;
51+
* // returns true
52+
*/
53+
54+
// MODULES //
55+
56+
var iterUnion = require( './main.js' );
57+
58+
59+
// EXPORTS //
60+
61+
module.exports = iterUnion;

0 commit comments

Comments
 (0)