Skip to content

Commit

Permalink
feat(iter): add iterCuNone function
Browse files Browse the repository at this point in the history
Implement iterCuNone to create an iterator which cumulatively tests
whether every iterated value is falsy. This new function:

- Takes an input iterator
- Returns a new iterator that yields boolean values
- Continues yielding  while all values are falsy
- Yields  once a truthy value is encountered
- Handles both iterable and non-iterable input iterators

Includes:
- Main implementation
- TypeScript typings
- Tests
- Benchmarks
- Example usage
- Documentation

Closes stdlib-js#2333
  • Loading branch information
ruthwikchikoti committed Jul 16, 2024
1 parent 2786956 commit e869302
Show file tree
Hide file tree
Showing 10 changed files with 949 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/iter/cunone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
@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.
-->

# iterCuNone

> Create an iterator 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' );
111 changes: 111 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,111 @@
/**
* @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.
*/

'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 )
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;


// EXPORTS //

export = iterCuNone;
68 changes: 68 additions & 0 deletions lib/node_modules/@stdlib/iter/cunone/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* @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.
*/

import iterCuNone = require( './index' );

/**
* Returns an iterator protocol-compliant object.
*
* @returns iterator protocol-compliant object
*/
function iterator() {
return {
'next': next
};

/**
* Implements the iterator protocol `next` method.
*
* @returns iterator protocol-compliant object
*/
function next() {
return {
'value': true,
'done': false
};
}
}


// TESTS //

// The function returns an iterator...
{
iterCuNone( iterator() ); // $ExpectType Iterator
}

// The compiler throws an error if the function is provided a value other than an iterator protocol-compliant object...
{
iterCuNone( '5' ); // $ExpectError
iterCuNone( 5 ); // $ExpectError
iterCuNone( true ); // $ExpectError
iterCuNone( false ); // $ExpectError
iterCuNone( null ); // $ExpectError
iterCuNone( undefined ); // $ExpectError
iterCuNone( [] ); // $ExpectError
iterCuNone( {} ); // $ExpectError
iterCuNone( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
iterCuNone(); // $ExpectError
}
46 changes: 46 additions & 0 deletions lib/node_modules/@stdlib/iter/cunone/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @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.
*/

'use strict';

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

// 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 );
}
Loading

0 comments on commit e869302

Please sign in to comment.