Skip to content

Commit 4226a90

Browse files
committed
Add iterator utility to return unique values
1 parent 6688f0a commit 4226a90

File tree

8 files changed

+1043
-0
lines changed

8 files changed

+1043
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
# iterUnique
22+
23+
> Create an [iterator][mdn-iterator-protocol] which returns unique 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 iterUnique = require( '@stdlib/iter/unique' );
41+
```
42+
43+
#### iterUnique( iterator )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which returns unique values.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var it = iterUnique( array2iterator( [ 2, 1, 1, 2, 4, 3, 4, 3 ] ) );
51+
// returns <Object>
52+
53+
var v = it.next().value;
54+
// returns 2
55+
56+
v = it.next().value;
57+
// returns 1
58+
59+
v = it.next().value;
60+
// returns 4
61+
62+
v = it.next().value;
63+
// returns 3
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+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
79+
80+
<section class="notes">
81+
82+
## Notes
83+
84+
- Value "uniqueness" is determined according to **strict equality**.
85+
- A returned [iterator][mdn-iterator-protocol] internally buffers unique values and, thus, has `O(N)` memory requirements.
86+
- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
87+
88+
</section>
89+
90+
<!-- /.notes -->
91+
92+
<!-- Package usage examples. -->
93+
94+
<section class="examples">
95+
96+
## Examples
97+
98+
<!-- eslint no-undef: "error" -->
99+
100+
```javascript
101+
var discreteUniform = require( '@stdlib/random/iter/discrete-uniform' );
102+
var iterUnique = require( '@stdlib/iter/unique' );
103+
104+
// Create a seeded iterator which can generate 1000 pseudorandom numbers:
105+
var rand = discreteUniform( 1, 10, {
106+
'seed': 1234,
107+
'iter': 1000
108+
});
109+
110+
// Create an iterator which returns unique values:
111+
var it = iterUnique( rand );
112+
113+
// Perform manual iteration...
114+
var v;
115+
while ( true ) {
116+
v = it.next();
117+
if ( v.done ) {
118+
break;
119+
}
120+
console.log( v.value );
121+
}
122+
```
123+
124+
</section>
125+
126+
<!-- /.examples -->
127+
128+
<!-- 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. -->
129+
130+
<section class="references">
131+
132+
</section>
133+
134+
<!-- /.references -->
135+
136+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="links">
139+
140+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
141+
142+
</section>
143+
144+
<!-- /.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 iterUnique = 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 = iterUnique( 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 = iterUnique( 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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( iterator )
3+
Returns an iterator which returns unique values.
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.
9+
10+
If an environment supports Symbol.iterator and a provided iterator is
11+
iterable, the returned iterator is iterable.
12+
13+
Parameters
14+
----------
15+
iterator: Object
16+
Input iterator.
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 it1 = {{alias:@stdlib/array/to-iterator}}( [ 1, 2, 1, 2, 4 ] );
34+
> var it2 = {{alias}}( it1 );
35+
> var v = it2.next().value
36+
1
37+
> v = it2.next().value
38+
2
39+
> v = it2.next().value
40+
4
41+
> var bool = it2.next().done
42+
true
43+
44+
See Also
45+
--------
46+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 iterUnique = require( './../lib' );
23+
24+
// Create a seeded iterator which can generate 1000 pseudorandom numbers:
25+
var rand = discreteUniform( 1, 10, {
26+
'seed': 1234,
27+
'iter': 1000
28+
});
29+
30+
// Create an iterator which returns unique values:
31+
var it = iterUnique( rand );
32+
33+
// Perform manual iteration...
34+
var v;
35+
while ( true ) {
36+
v = it.next();
37+
if ( v.done ) {
38+
break;
39+
}
40+
console.log( v.value );
41+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 unique values.
23+
*
24+
* @module @stdlib/iter/unique
25+
*
26+
* @example
27+
* var array2iterator = require( '@stdlib/array/to-iterator' );
28+
* var iterUnique = require( '@stdlib/iter/unique' );
29+
*
30+
* var iter = iterUnique( array2iterator( [ 1, 2, 1, 2, 4 ] ) );
31+
*
32+
* var v = iter.next().value;
33+
* // returns 1
34+
*
35+
* v = iter.next().value;
36+
* // returns 2
37+
*
38+
* v = iter.next().value;
39+
* // returns 4
40+
*
41+
* var bool = iter.next().done;
42+
* // returns true
43+
*/
44+
45+
// MODULES //
46+
47+
var iterUnique = require( './main.js' );
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = iterUnique;

0 commit comments

Comments
 (0)