Skip to content

Commit 5fffd5a

Browse files
committed
Add iterator utility to deduplicate consecutive values
1 parent a14b164 commit 5fffd5a

File tree

10 files changed

+1486
-0
lines changed

10 files changed

+1486
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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 -->
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 iterDedupe = 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 = iterDedupe( 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 = iterDedupe( 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( iterator[, limit] )
3+
Returns an iterator which removes consecutive duplicated values.
4+
5+
`NaN` values are considered distinct.
6+
7+
If an environment supports Symbol.iterator and a provided iterator is
8+
iterable, the returned iterator is iterable.
9+
10+
Parameters
11+
----------
12+
iterator: Object
13+
Input iterator.
14+
15+
limit: integer (optional)
16+
Number of allowed consecutive duplicates. Default: 1.
17+
18+
Returns
19+
-------
20+
iterator: Object
21+
Iterator.
22+
23+
iterator.next(): Function
24+
Returns an iterator protocol-compliant object containing the next
25+
iterated value (if one exists) and a boolean flag indicating whether the
26+
iterator is finished.
27+
28+
iterator.return( [value] ): Function
29+
Finishes an iterator and returns a provided value.
30+
31+
Examples
32+
--------
33+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 1, 1, 2, 3, 3 ] );
34+
> var it = {{alias}}( arr );
35+
> var v = it.next().value
36+
1
37+
> v = it.next().value
38+
2
39+
> v = it.next().value
40+
3
41+
42+
See Also
43+
--------
44+

0 commit comments

Comments
 (0)