Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add iter/cunone #2616

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions lib/node_modules/@stdlib/iter/cunone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs updating. It does not match project conventions.

# iterCuNone

> Create an [iterator][mdn-iterator-protocol] which cumulatively tests whether every iterated value is falsy.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage
```javascript
var iterCuNone = require( '@stdlib/iter/cunone' );
```
```javascript
iterCuNone( iterator )
Returns an iterator which cumulatively tests whether every iterated value is falsy.
javascriptvar array2iterator = require( '@stdlib/array/to-iterator' );

var arr = array2iterator( [ false, null, undefined, '', 0, NaN, true ] );

var it = iterCuNone( arr );
// returns <Object>

var v = it.next().value;
// returns true

v = it.next().value;
// returns true

v = it.next().value;
// returns true

v = it.next().value;
// returns true

v = it.next().value;
// returns true

v = it.next().value;
// returns true

v = it.next().value;
// returns false

var bool = it.next().done;
// returns true
```
The returned iterator protocol-compliant object has the following properties:

next: function which returns an iterator 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 is finished.
return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.

</section>
<!-- /.usage -->
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">

## Notes



- If an environment supports `Symbol.iterator` and the provided iterator is iterable, the returned iterator is iterable.
- In JavaScript, falsy values are `false`, `null`, `undefined`, `0`, `NaN`, and an empty string (`""`).

</section>
<!-- /.notes -->
<!-- Package usage examples. -->
<section class="examples">

## Examples
<!-- eslint no-undef: "error" -->
```javascript

var randu = require( '@stdlib/random/iter/randu' );
var iterMap = require( '@stdlib/iter/map' );
var iterCuNone = require( '@stdlib/iter/cunone' );

// Create an iterator which generates uniformly distributed pseudorandom numbers:
var rand = randu({
'iter': 100
});

// Create an iterator which applies a threshold to generated numbers:
var it = iterMap( rand, function threshold( r ) {
return ( r > 0.95 );
});

// Create an iterator which cumulatively tests whether all values are "falsy":
var result = iterCuNone( it );

// Perform manual iteration...
var v;
while ( true ) {
v = result.next();
if ( v.done ) {
break;
}
console.log( v.value );
}
```
</section>
<!-- /.examples -->
<!-- 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. -->
<section class="references">
</section>
<!-- /.references -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">



</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="links">
<!-- <related-links> -->
<!-- </related-links> -->
</section>
<!-- /.links -->
96 changes: 96 additions & 0 deletions lib/node_modules/@stdlib/iter/cunone/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/** * @license Apache-2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how we do license headers.

* * * Copyright (c) 2024 The Stdlib Authors. * *
* Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;
var isIteratorLike = require( '@stdlib/assert-is-iterator-like' );
var pkg = require( './../package.json' ).name;
var iterCuNone = require( './../lib' );

// FUNCTIONS //

function createIterator( arr ) {
var len;
var it;
var i;

len = arr.length;
i = -1;
it = {};
it.next = next;
it.reset = reset;

return it;

function next() {
i += 1;
if ( i < len ) {
return { 'value': arr[ i ], 'done': false };
}
return { 'done': true };
}

function reset() {
i = -1;
}
}

// MAIN //

bench( pkg, function benchmark( b ) {
var iter;
var arr;
var i;

arr = [ 0, 0, 0, 0, 0, 1 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
iter = iterCuNone( createIterator( arr ) );
if ( typeof iter !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();

if ( !isIteratorLike( iter ) ) {
b.fail( 'should return an iterator protocol-compliant object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::iteration', function benchmark( b ) {
var iter;
var arr;
var i;
var v;

arr = [ 0, 0, 0, 0, 0, 1 ];
iter = iterCuNone( createIterator( arr ) );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = iter.next().value;
if ( typeof v !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();

if ( !isBoolean( v ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
53 changes: 53 additions & 0 deletions lib/node_modules/@stdlib/iter/cunone/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{{alias}}( iterator )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how we format REPL text files.

Returns an iterator which cumulatively tests whether every iterated value is
falsy.

The returned iterator immediately returns `false` upon encountering a truthy
value, for all subsequent iterations.

If provided an iterator which does not return any iterated values, the
returned iterator returns `true`.

Parameters
----------
iterator: Object
Input iterator over which to iterate.

Returns
-------
iterator: Object
Iterator.

Iterator Protocol
-----------------
The returned iterator protocol-compliant object has the following properties:

next: Function
Returns an iterator 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
is finished.

return: Function
Finishes an iterator and returns a single (optional) argument in an
iterator protocol-compliant object.

Examples
--------
> var arr = {{alias:@stdlib/array/to-iterator}}( [ 0, 0, 0, 0, 1 ] );
> var it = {{alias}}( arr )
> var v = it.next().value
true
> v = it.next().value
true
> v = it.next().value
true
> v = it.next().value
true
> v = it.next().value
false
> var bool = it.next().done
true

See Also
--------
69 changes: 69 additions & 0 deletions lib/node_modules/@stdlib/iter/cunone/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';

// Define a union type representing both iterable and non-iterable iterators:
type Iterator = Iter | IterableIterator;

/**
* Returns an iterator which cumulatively tests whether every iterated value is falsy.
*
* @param iterator - input iterator
* @returns iterator
*
* @example
* var array2iterator = require( '@stdlib/array-to-iterator' );
*
* var it = iterCuNone( array2iterator( [ false, null, undefined, '', 0, NaN, true ] ) );
* // returns <Object>
*
* var v = it.next().value;
* // returns true
*
* v = it.next().value;
* // returns true
*
* v = it.next().value;
* // returns true
*
* v = it.next().value;
* // returns true
*
* v = it.next().value;
* // returns true
*
* v = it.next().value;
* // returns true
*
* v = it.next().value;
* // returns false
*
* var bool = it.next().done;
* // returns true
*/
declare function iterCuNone( iterator: Iterator ): Iterator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type can be improved. Use TypedIterator from types/iter.



// EXPORTS //

export = iterCuNone;

Check failure on line 69 in lib/node_modules/@stdlib/iter/cunone/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Newline required at end of file but not found
Loading
Loading