Skip to content

Commit d07da86

Browse files
RidamGargkgryte
andauthored
feat: add iter/cuany
PR-URL: #2440 Closes: #2331 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent c41435f commit d07da86

File tree

10 files changed

+1053
-0
lines changed

10 files changed

+1053
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
# iterCuAny
22+
23+
> Create an [iterator][mdn-iterator-protocol] which cumulatively tests whether at least one iterated value is truthy.
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 iterCuAny = require( '@stdlib/iter/cuany' );
41+
```
42+
43+
#### iterCuAny( iterator )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether at least one iterated value is truthy.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var arr = array2iterator( [ 0, 0, 0, 1, 0 ] );
51+
52+
var it = iterCuAny( arr );
53+
// returns <Object>
54+
55+
var v = it.next().value;
56+
// returns false
57+
58+
v = it.next().value;
59+
// returns false
60+
61+
v = it.next().value;
62+
// returns false
63+
64+
v = it.next().value;
65+
// returns true
66+
67+
v = it.next().value;
68+
// returns true
69+
70+
var bool = it.next().done;
71+
// returns true
72+
```
73+
74+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
75+
76+
- **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.
77+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
84+
85+
<section class="notes">
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<!-- Package usage examples. -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var randu = require( '@stdlib/random/iter/randu' );
101+
var iterMap = require( '@stdlib/iter/map' );
102+
var iterCuAny = require( '@stdlib/iter/cuany' );
103+
104+
function threshold( r ) {
105+
return ( r > 0.95 );
106+
}
107+
108+
// Create an iterator which generates uniformly distributed pseudorandom numbers:
109+
var opts = {
110+
'iter': 100
111+
};
112+
var riter = randu( opts );
113+
114+
// Create an iterator which applies a threshold to generated numbers:
115+
var miter = iterMap( riter, threshold );
116+
117+
// Create an iterator which cumulatively tests whether at least one iterated value is truthy:
118+
var it = iterCuAny( miter );
119+
120+
// Perform manual iteration...
121+
var r;
122+
while ( true ) {
123+
r = it.next();
124+
if ( r.done ) {
125+
break;
126+
}
127+
console.log( r.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 related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
156+
157+
</section>
158+
159+
<!-- /.links -->
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var iterCuAny = 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 = iterCuAny( 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 = iterCuAny( iterConstant( 3.14 ) );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
v = iter.next().value;
65+
if ( isnan( v ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( v ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( iterator )
3+
Returns an iterator which cumulatively tests whether at least one iterated
4+
value is truthy.
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+
false
33+
> v = it.next().value
34+
false
35+
> v = it.next().value
36+
false
37+
> v = it.next().value
38+
true
39+
> v = it.next().value
40+
true
41+
42+
See Also
43+
--------
44+
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 at least one iterated value is truthy.
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 = iterCuAny( arr );
40+
*
41+
* var v = it.next().value;
42+
* returns false
43+
*
44+
* v = it.next().value;
45+
* returns false
46+
*
47+
* v = it.next().value;
48+
* returns false
49+
*
50+
* v = it.next().value;
51+
* returns true
52+
*
53+
* v = it.next().value;
54+
* returns true
55+
*
56+
* var bool = it.next().done;
57+
* returns true
58+
*/
59+
declare function iterCuAny( iterator: Iterator ): Iterator;
60+
61+
62+
// EXPORTS //
63+
64+
export = iterCuAny;

0 commit comments

Comments
 (0)