Skip to content

Commit aa707da

Browse files
TheNourhankgryte
andauthored
feat: add iter/cusome
PR-URL: #2561 Closes: #2334 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 9d0ce8b commit aa707da

File tree

10 files changed

+1075
-0
lines changed

10 files changed

+1075
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
# iterCuSome
22+
23+
> Create an iterator which cumulatively tests whether at least `n` iterated values are 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 iterCuSome = require( '@stdlib/iter/cusome' );
41+
```
42+
43+
#### iterCuSome( iterator, n )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether at least `n` iterated values are truthy.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var arr = array2iterator( [ 0, 0, 1, 1, 0 ] );
51+
52+
var it = iterCuSome( arr, 2 );
53+
54+
var v = it.next().value;
55+
// returns false
56+
57+
v = it.next().value;
58+
// returns false
59+
60+
v = it.next().value;
61+
// returns false
62+
63+
v = it.next().value;
64+
// returns true
65+
66+
v = it.next().value;
67+
// returns true
68+
69+
var bool = it.next().done;
70+
// returns true
71+
```
72+
73+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
74+
75+
- **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.
76+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
83+
84+
<section class="notes">
85+
86+
</section>
87+
88+
<!-- /.notes -->
89+
90+
<!-- Package usage examples. -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var randu = require( '@stdlib/random/iter/randu' );
100+
var iterMap = require( '@stdlib/iter/map' );
101+
var iterCuSome = require( '@stdlib/iter/cusome' );
102+
103+
function threshold( r ) {
104+
return ( r > 0.95 );
105+
}
106+
107+
// Create an iterator which generates uniformly distributed pseudorandom numbers:
108+
var opts = {
109+
'iter': 100
110+
};
111+
var riter = randu( opts );
112+
113+
// Create an iterator which applies a threshold to generated numbers:
114+
var miter = iterMap( riter, threshold );
115+
116+
// Create an iterator which cumulatively tests whether at least 5 iterated values are truthy:
117+
var it = iterCuSome( miter, 5 );
118+
119+
// Perform manual iteration...
120+
var r;
121+
while ( true ) {
122+
r = it.next();
123+
if ( r.done ) {
124+
break;
125+
}
126+
console.log( r.value );
127+
}
128+
```
129+
130+
</section>
131+
132+
<!-- /.examples -->
133+
134+
<!-- 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. -->
135+
136+
<section class="references">
137+
138+
</section>
139+
140+
<!-- /.references -->
141+
142+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
143+
144+
<section class="related">
145+
146+
</section>
147+
148+
<!-- /.related -->
149+
150+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
151+
152+
<section class="links">
153+
154+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
155+
156+
</section>
157+
158+
<!-- /.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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
26+
var iterConstant = require( '@stdlib/iter/constant' );
27+
var pkg = require( './../package.json' ).name;
28+
var iterCuSome = 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 = iterCuSome( it, 2 );
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 = iterCuSome( iterConstant( 3.14 ), b.iterations );
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
{{alias}}( iterator, n )
3+
Returns an iterator which cumulatively tests whether at least `n` iterated
4+
values are 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+
n: integer
15+
Minimum number of truthy values.
16+
17+
Returns
18+
-------
19+
iterator: Object
20+
Iterator.
21+
22+
iterator.next(): Function
23+
Returns an iterator protocol-compliant object containing the next
24+
iterated value (if one exists) and a boolean flag indicating whether the
25+
iterator is finished.
26+
27+
iterator.return( [value] ): Function
28+
Finishes an iterator and returns a provided value.
29+
30+
Examples
31+
--------
32+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 0, 0, 1, 1, 0 ] );
33+
> var it = {{alias}}( arr, 2 );
34+
> var v = it.next().value
35+
false
36+
> v = it.next().value
37+
false
38+
> v = it.next().value
39+
false
40+
> v = it.next().value
41+
true
42+
> v = it.next().value
43+
true
44+
45+
See Also
46+
--------
47+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 `n` iterated values are truthy.
30+
*
31+
* @param iterator - source iterator
32+
* @param n - minimum number of truthy elements
33+
* @returns iterator
34+
*
35+
* @example
36+
* var array2iterator = require( '@stdlib/array/to-iterator' );
37+
*
38+
* var it = iterCuSome( array2iterator( [ false, false, false, true, true, false ] ), 2 );
39+
*
40+
* var v = it.next().value;
41+
* // returns false
42+
*
43+
* v = it.next().value;
44+
* // returns false
45+
*
46+
* v = it.next().value;
47+
* // returns false
48+
*
49+
* v = it.next().value;
50+
* // returns false
51+
*
52+
* v = it.next().value;
53+
* // returns true
54+
*
55+
* v = it.next().value;
56+
* // returns true
57+
*
58+
* // ..
59+
*/
60+
declare function iterCuSome( iterator: Iterator, n: number ): Iterator;
61+
62+
63+
// EXPORTS //
64+
65+
export = iterCuSome;

0 commit comments

Comments
 (0)