|
| 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 | +# iterDedupe |
| 22 | + |
| 23 | +> Create an [iterator][mdn-iterator-protocol] which iteratively computes the number of [iterated][mdn-iterator-protocol] values. |
| 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 iterDedupe = require( '@stdlib/iter/dedupe' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### iterDedupe( iterator\[, limit] ) |
| 44 | + |
| 45 | +Returns an [iterator][mdn-iterator-protocol] which removes consecutive duplicated values. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 49 | + |
| 50 | +var it = iterDedupe( array2iterator( [ 1, 1, 2, 3, 3, 3, 4, 4 ] ) ); |
| 51 | +// returns <Object> |
| 52 | + |
| 53 | +var v = it.next().value; |
| 54 | +// returns 1 |
| 55 | + |
| 56 | +v = it.next().value; |
| 57 | +// returns 2 |
| 58 | + |
| 59 | +v = it.next().value; |
| 60 | +// returns 3 |
| 61 | + |
| 62 | +v = it.next().value; |
| 63 | +// returns 4 |
| 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 | +The returned [iterator][mdn-iterator-protocol] removes **consecutive** duplicated values and does **not** returned globally unique values. |
| 75 | + |
| 76 | +```javascript |
| 77 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 78 | + |
| 79 | +var it = iterDedupe( array2iterator( [ 1, 1, 2, 1, 1, 2 ] ) ); |
| 80 | +// returns <Object> |
| 81 | + |
| 82 | +var v = it.next().value; |
| 83 | +// returns 1 |
| 84 | + |
| 85 | +v = it.next().value; |
| 86 | +// returns 2 |
| 87 | + |
| 88 | +v = it.next().value; |
| 89 | +// returns 1 |
| 90 | + |
| 91 | +v = it.next().value; |
| 92 | +// returns 2 |
| 93 | + |
| 94 | +var bool = it.next().done; |
| 95 | +// returns true |
| 96 | +``` |
| 97 | + |
| 98 | +To specify the number of allowed consecutive duplicated values, provide a second argument. |
| 99 | + |
| 100 | +```javascript |
| 101 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 102 | + |
| 103 | +var it = iterDedupe( array2iterator( [ 1, 1, 2, 3, 3, 3, 3, 4, 4, 4 ] ), 2 ); |
| 104 | +// returns <Object> |
| 105 | + |
| 106 | +var v = it.next().value; |
| 107 | +// returns 1 |
| 108 | + |
| 109 | +v = it.next().value; |
| 110 | +// returns 1 |
| 111 | + |
| 112 | +v = it.next().value; |
| 113 | +// returns 2 |
| 114 | + |
| 115 | +v = it.next().value; |
| 116 | +// returns 3 |
| 117 | + |
| 118 | +v = it.next().value; |
| 119 | +// returns 3 |
| 120 | + |
| 121 | +v = it.next().value; |
| 122 | +// returns 4 |
| 123 | + |
| 124 | +v = it.next().value; |
| 125 | +// returns 4 |
| 126 | + |
| 127 | +var bool = it.next().done; |
| 128 | +// returns true |
| 129 | +``` |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.usage --> |
| 134 | + |
| 135 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 136 | + |
| 137 | +<section class="notes"> |
| 138 | + |
| 139 | +## Notes |
| 140 | + |
| 141 | +- `NaN` values are considered **distinct**. |
| 142 | + |
| 143 | + ```javascript |
| 144 | + var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 145 | + |
| 146 | + var it = iterDedupe( array2iterator( [ NaN, NaN, NaN, NaN ] ) ); |
| 147 | + // returns <Object> |
| 148 | + |
| 149 | + var v = it.next().value; |
| 150 | + // returns NaN |
| 151 | + |
| 152 | + v = it.next().value; |
| 153 | + // returns NaN |
| 154 | + |
| 155 | + v = it.next().value; |
| 156 | + // returns NaN |
| 157 | + |
| 158 | + v = it.next().value; |
| 159 | + // returns NaN |
| 160 | + |
| 161 | + var bool = it.next().done; |
| 162 | + // returns true |
| 163 | + ``` |
| 164 | + |
| 165 | +- Uniqueness is determined according to strict equality. Accordingly, object's are **not** checked for deep equality. |
| 166 | +
|
| 167 | + ```javascript |
| 168 | + var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 169 | +
|
| 170 | + var it = iterDedupe( array2iterator( [ {}, {}, {}, {} ] ) ); |
| 171 | + // returns <Object> |
| 172 | +
|
| 173 | + var v = it.next().value; |
| 174 | + // returns {} |
| 175 | +
|
| 176 | + v = it.next().value; |
| 177 | + // returns {} |
| 178 | +
|
| 179 | + v = it.next().value; |
| 180 | + // returns {} |
| 181 | +
|
| 182 | + v = it.next().value; |
| 183 | + // returns {} |
| 184 | +
|
| 185 | + var bool = it.next().done; |
| 186 | + // returns true |
| 187 | + ``` |
| 188 | +
|
| 189 | +- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable. |
| 190 | +
|
| 191 | +</section> |
| 192 | +
|
| 193 | +<!-- /.notes --> |
| 194 | +
|
| 195 | +<!-- Package usage examples. --> |
| 196 | +
|
| 197 | +<section class="examples"> |
| 198 | +
|
| 199 | +## Examples |
| 200 | +
|
| 201 | +<!-- eslint no-undef: "error" --> |
| 202 | +
|
| 203 | +```javascript |
| 204 | +var randi = require( '@stdlib/random/iter/discrete-uniform' ); |
| 205 | +var iterDedupe = require( '@stdlib/iter/dedupe' ); |
| 206 | +
|
| 207 | +// Create a seeded iterator for generating pseudorandom integers on the interval [1,3]: |
| 208 | +var rand = randi( 1, 3, { |
| 209 | + 'seed': 1234, |
| 210 | + 'iter': 100 |
| 211 | +}); |
| 212 | +
|
| 213 | +// Create an iterator which limits "runs" to at most 3 values: |
| 214 | +var it = iterDedupe( rand, 3 ); |
| 215 | +
|
| 216 | +// Perform manual iteration... |
| 217 | +var v; |
| 218 | +while ( true ) { |
| 219 | + v = it.next(); |
| 220 | + if ( v.done ) { |
| 221 | + break; |
| 222 | + } |
| 223 | + console.log( v.value ); |
| 224 | +} |
| 225 | +``` |
| 226 | +
|
| 227 | +</section> |
| 228 | +
|
| 229 | +<!-- /.examples --> |
| 230 | +
|
| 231 | +<!-- 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. --> |
| 232 | +
|
| 233 | +<section class="references"> |
| 234 | +
|
| 235 | +</section> |
| 236 | +
|
| 237 | +<!-- /.references --> |
| 238 | +
|
| 239 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 240 | +
|
| 241 | +<section class="links"> |
| 242 | +
|
| 243 | +[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol |
| 244 | +
|
| 245 | +</section> |
| 246 | +
|
| 247 | +<!-- /.links --> |
0 commit comments