Skip to content

Commit 783c5d6

Browse files
feat: add iter/cunone
PR-URL: #2978 Closes: #2333 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 4927336 commit 783c5d6

File tree

10 files changed

+1054
-0
lines changed

10 files changed

+1054
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# iterCuNone
22+
23+
> Create an [iterator][mdn-iterator-protocol] which cumulatively tests whether every iterated value is falsy.
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 iterCuNone = require( '@stdlib/iter/cunone' );
41+
```
42+
43+
#### iterCuNone( iterator )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether every
46+
iterated value is falsy.
47+
48+
```javascript
49+
var array2iterator = require( '@stdlib/array/to-iterator' );
50+
51+
var arr = array2iterator( [ 0, 0, 0, 1, 0 ] );
52+
53+
var it = iterCuNone( arr );
54+
// returns <Object>
55+
56+
var v = it.next().value;
57+
// returns true
58+
59+
v = it.next().value;
60+
// returns true
61+
62+
v = it.next().value;
63+
// returns true
64+
65+
v = it.next().value;
66+
// returns false
67+
68+
v = it.next().value;
69+
// returns false
70+
71+
var bool = it.next().done;
72+
// returns true
73+
```
74+
75+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
76+
77+
- **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.
78+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
85+
86+
<section class="notes">
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 randu = require( '@stdlib/random/iter/randu' );
102+
var iterMap = require( '@stdlib/iter/map' );
103+
var iterCuNone = require( '@stdlib/iter/cunone' );
104+
105+
function threshold( r ) {
106+
return ( r > 0.95 );
107+
}
108+
109+
// Create an iterator which generates uniformly distributed pseudorandom numbers:
110+
var opts = {
111+
'iter': 100
112+
};
113+
var riter = randu( opts );
114+
115+
// Create an iterator which applies a threshold to generated numbers:
116+
var miter = iterMap( riter, threshold );
117+
118+
// Create an iterator which cumulatively tests whether every iterated value is falsy.
119+
var it = iterCuNone( miter );
120+
121+
// Perform manual iteration...
122+
var r;
123+
while ( true ) {
124+
r = it.next();
125+
if ( r.done ) {
126+
break;
127+
}
128+
console.log( r.value );
129+
}
130+
```
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
<!-- 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. -->
137+
138+
<section class="references">
139+
140+
</section>
141+
142+
<!-- /.references -->
143+
144+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145+
146+
<section class="related">
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
157+
158+
</section>
159+
160+
<!-- /.links -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 iterConstant = require( '@stdlib/iter/constant' );
25+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var iterCuNone = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var iter;
35+
var it;
36+
var i;
37+
38+
it = iterConstant( 3.14 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = iterCuNone( it );
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 iter;
57+
var v;
58+
var i;
59+
60+
iter = iterCuNone( iterConstant( 3.14 ) );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
v = iter.next().value;
65+
if ( !isBoolean( v ) ) {
66+
b.fail( 'should return a boolean' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isBoolean( v ) ) {
71+
b.fail( 'should return a boolean' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( iterator )
3+
Returns an iterator which cumulatively tests whether every iterated value is
4+
falsy.
5+
6+
If an environment supports Symbol.iterator and a provided iterator is
7+
iterable, the returned iterator is iterable.
8+
9+
Parameters
10+
----------
11+
iterator: Object
12+
Input iterator.
13+
14+
Returns
15+
-------
16+
iterator: Object
17+
Iterator.
18+
19+
iterator.next(): Function
20+
Returns an iterator protocol-compliant object containing the next
21+
iterated value (if one exists) and a boolean flag indicating whether the
22+
iterator is finished.
23+
24+
iterator.return( [value] ): Function
25+
Finishes an iterator and returns a provided value.
26+
27+
Examples
28+
--------
29+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 0, 0, 0, 1, 0 ] );
30+
> var it = {{alias}}( arr );
31+
> var v = it.next().value
32+
true
33+
> v = it.next().value
34+
true
35+
> v = it.next().value
36+
true
37+
> v = it.next().value
38+
false
39+
> v = it.next().value
40+
false
41+
42+
See Also
43+
--------
44+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
24+
25+
// Define a union type representing both iterable and non-iterable iterators:
26+
type Iterator = Iter | IterableIterator;
27+
28+
/**
29+
* Returns an iterator which cumulatively tests whether every iterated value is falsy.
30+
*
31+
* @param iterator - input iterator
32+
* @returns iterator
33+
*
34+
* @example
35+
* var array2iterator = require( '@stdlib/array/to-iterator' );
36+
*
37+
* var arr = array2iterator( [ false, false, false, true, false ] );
38+
*
39+
* var it = iterCuNone( arr );
40+
*
41+
* var v = it.next().value;
42+
* returns true
43+
*
44+
* v = it.next().value;
45+
* returns true
46+
*
47+
* v = it.next().value;
48+
* returns true
49+
*
50+
* v = it.next().value;
51+
* returns false
52+
*
53+
* v = it.next().value;
54+
* returns false
55+
*
56+
* var bool = it.next().done;
57+
* returns true
58+
*/
59+
declare function iterCuNone( iterator: Iterator ): Iterator;
60+
61+
62+
// EXPORTS //
63+
64+
export = iterCuNone;

0 commit comments

Comments
 (0)