|
| 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 --> |
0 commit comments