-
-
Notifications
You must be signed in to change notification settings - Fork 447
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
Feature/iter cunone #2613
Closed
ruthwikchikoti
wants to merge
2
commits into
stdlib-js:develop
from
ruthwikchikoti:feature/iter-cunone
Closed
Feature/iter cunone #2613
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
111
lib/node_modules/@stdlib/iter/cunone/benchmark/benchmark.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure you have EditorConfig setup as documented in the dev guides. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't correct. |
||
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
69
lib/node_modules/@stdlib/iter/cunone/docs/types/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is incomplete.