Skip to content

Commit c0de83a

Browse files
Kaif987kgryte
andauthored
feat: add iter/cunone-by
PR-URL: #2783 Closes: #2337 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ab0faa5 commit c0de83a

File tree

10 files changed

+1260
-0
lines changed

10 files changed

+1260
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
# iterCuNoneBy
22+
23+
> Create an iterator which cumulatively tests whether every iterated value fails a test implemented by a predicate function.
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 iterCuNoneBy = require( '@stdlib/iter/cunone-by' );
41+
```
42+
43+
#### iterCuNoneBy( iterator, predicate\[, thisArg] )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether every iterated value fails a test implemented by a `predicate` function.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
function predicate( v ) {
51+
return ( v > 0 );
52+
}
53+
54+
var arr = array2iterator( [ 0, 0, 0, 1, 0 ] );
55+
56+
var it = iterCuNoneBy( arr, predicate );
57+
58+
var v = it.next().value;
59+
// returns true
60+
61+
v = it.next().value;
62+
// returns true
63+
64+
v = it.next().value;
65+
// returns true
66+
67+
v = it.next().value;
68+
// returns false
69+
70+
v = it.next().value;
71+
// returns false
72+
73+
var bool = it.next().done;
74+
// returns true
75+
```
76+
77+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
78+
79+
- **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.
80+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
81+
82+
A `predicate` function is provided two arguments:
83+
84+
- **value**: iterated value
85+
- **index**: iteration index (zero-based)
86+
87+
To set the `predicate` function execution context, provide a `thisArg`.
88+
89+
<!-- eslint-disable no-invalid-this -->
90+
91+
```javascript
92+
var array2iterator = require( '@stdlib/array/to-iterator' );
93+
94+
function predicate( v ) {
95+
this.count += 1;
96+
return ( v < 0 );
97+
}
98+
99+
var ctx = {
100+
'count': 0
101+
};
102+
103+
var it = iterCuNoneBy( array2iterator( [ 1, 2, 3, 4 ] ), predicate, ctx );
104+
// returns <Object>
105+
106+
var v = it.next().value;
107+
// returns true
108+
109+
v = it.next().value;
110+
// returns true
111+
112+
v = it.next().value;
113+
// returns true
114+
115+
v = it.next().value;
116+
// returns true
117+
118+
var count = ctx.count;
119+
// returns 4
120+
```
121+
122+
</section>
123+
124+
<!-- /.usage -->
125+
126+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
127+
128+
<section class="notes">
129+
130+
## Notes
131+
132+
- A `predicate` function is invoked for each iterated value until the first truthy `predicate` function return value. Upon encountering the first truthy return value, the returned iterator ceases to invoke the `predicate` function and returns `false` for each subsequent iterated value of the provided input `iterator`.
133+
134+
</section>
135+
136+
<!-- /.notes -->
137+
138+
<!-- Package usage examples. -->
139+
140+
<section class="examples">
141+
142+
## Examples
143+
144+
<!-- eslint no-undef: "error" -->
145+
146+
```javascript
147+
var randu = require( '@stdlib/random/iter/randu' );
148+
var iterCuNoneBy = require( '@stdlib/iter/cunone-by' );
149+
150+
function threshold( r ) {
151+
return ( r > 0.95 );
152+
}
153+
154+
// Create an iterator which generates uniformly distributed pseudorandom numbers:
155+
var opts = {
156+
'iter': 100
157+
};
158+
var riter = randu( opts );
159+
160+
// Create an iterator which cumulatively tests whether every iterated value fails a test:
161+
var it = iterCuNoneBy( riter, threshold );
162+
163+
// Perform manual iteration...
164+
var r;
165+
while ( true ) {
166+
r = it.next();
167+
if ( r.done ) {
168+
break;
169+
}
170+
console.log( r.value );
171+
}
172+
```
173+
174+
</section>
175+
176+
<!-- /.examples -->
177+
178+
<!-- 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. -->
179+
180+
<section class="references">
181+
182+
</section>
183+
184+
<!-- /.references -->
185+
186+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
187+
188+
<section class="related">
189+
190+
</section>
191+
192+
<!-- /.related -->
193+
194+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
195+
196+
<section class="links">
197+
198+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
199+
200+
</section>
201+
202+
<!-- /.links -->
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 iterCuNoneBy = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Predicate function.
35+
*
36+
* @private
37+
* @param {*} value - value
38+
* @returns {boolean} result
39+
*/
40+
function predicate( value ) {
41+
return ( value < 0 );
42+
}
43+
44+
45+
// MAIN //
46+
47+
bench( pkg, function benchmark( b ) {
48+
var iter;
49+
var it;
50+
var i;
51+
52+
it = iterConstant( 3.14 );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
iter = iterCuNoneBy( it, predicate );
57+
if ( typeof iter !== 'object' ) {
58+
b.fail( 'should return an object' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isIteratorLike( iter ) ) {
63+
b.fail( 'should return an iterator protocol-compliant object' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
68+
69+
bench( pkg+'::iteration', function benchmark( b ) {
70+
var iter;
71+
var v;
72+
var i;
73+
74+
iter = iterCuNoneBy( iterConstant( 3.14 ), predicate );
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
v = iter.next().value;
79+
if ( !isBoolean( v ) ) {
80+
b.fail( 'should return a boolean' );
81+
}
82+
}
83+
b.toc();
84+
if ( !isBoolean( v ) ) {
85+
b.fail( 'should return a boolean' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
{{alias}}( iterator, predicate[, thisArg] )
3+
Returns an iterator which cumulatively tests whether every iterated value
4+
fails a test implemented by a predicate function.
5+
6+
If an environment supports Symbol.iterator and a provided iterator is
7+
iterable, the returned iterator is iterable.
8+
9+
The predicate function is provided two arguments:
10+
11+
- value: iterated value
12+
- index: iteration index
13+
14+
A predicate function is invoked for each iterated value until the first
15+
truthy predicate function return value. Upon encountering the first truthy
16+
return value, the returned iterator ceases to invoke the predicate function
17+
and returns `false` for each subsequent iterated value of the provided input
18+
iterator.
19+
20+
If provided an iterator which does not return any iterated values, the
21+
function returns an iterator which is also empty.
22+
23+
Parameters
24+
----------
25+
iterator: Object
26+
Input iterator.
27+
28+
predicate: Function
29+
The test function.
30+
31+
thisArg: any (optional)
32+
Execution context.
33+
34+
Returns
35+
-------
36+
iterator: Object
37+
Iterator.
38+
39+
iterator.next(): Function
40+
Returns an iterator protocol-compliant object containing the next
41+
iterated value (if one exists) and a boolean flag indicating
42+
whether the iterator is finished.
43+
44+
iterator.return( [value] ): Function
45+
Finishes an iterator and returns a provided value.
46+
47+
Examples
48+
--------
49+
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 0, 0, 0, 1, 0 ] );
50+
> function fcn( v ) { return ( v > 0 ); };
51+
> var it = {{alias}}( arr, fcn );
52+
> var v = it.next().value
53+
true
54+
> v = it.next().value
55+
true
56+
> v = it.next().value
57+
true
58+
> v = it.next().value
59+
false
60+
> v = it.next().value
61+
false
62+
63+
See Also
64+
--------

0 commit comments

Comments
 (0)