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: Added C implementation for @stdlib/math/base/special/fibonacci-index #2673

Closed
Closed
238 changes: 238 additions & 0 deletions lib/node_modules/@stdlib/iter/cusome-by/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
Here's the adapted README.md for `cusome-by`, based on the `some-by` version:

```markdown
<!--

@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.

-->

# iterCuSomeBy

> Create an iterator which cumulatively tests whether at least `n` [iterated][mdn-iterator-protocol] values pass a test implemented by a predicate function.

<!-- 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 iterCuSomeBy = require( '@stdlib/iter/cusome-by' );
```

#### iterCuSomeBy( iterator, n, predicate\[, thisArg] )

Returns an iterator which cumulatively tests whether at least `n` [iterated][mdn-iterator-protocol] values **pass** a test implemented by a `predicate` function.

```javascript
var array2iterator = require( '@stdlib/array/to-iterator' );

function predicate( v ) {
return ( v > 0 );
}

var arr = array2iterator( [ 0, 0, 0, 1, 1 ] );

var it = iterCuSomeBy( arr, 2, predicate );
// returns <Object>

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

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

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

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

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

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.

A `predicate` function is provided two arguments:

- **value**: iterated value
- **index**: iteration index (zero-based)

To set the execution context of the `predicate` function, provide a `thisArg`.

```javascript
var array2iterator = require( '@stdlib/array/to-iterator' );

function predicate( v ) {
this.count += 1;
return ( v > 0 );
}

var arr = array2iterator( [ 0, 0, 1, 1, 1 ] );

var ctx = {
'count': 0
};

var it = iterCuSomeBy( arr, 3, predicate, ctx );
// returns <Object>

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

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

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

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

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

var bool = it.next().done;
// returns true

var n = ctx.count;
// returns 5
```

</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`, the returned iterator is iterable.
- The returned iterator continues iterating until it reaches the end of the input iterator, even after the condition is met.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/iter/randu' );
var iterCuSomeBy = require( '@stdlib/iter/cusome-by' );

function threshold( r ) {
return ( r >= 0.95 );
}

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

// Create an iterator which iteratively checks for values above a threshold:
var it = iterCuSomeBy( riter, 5, threshold );

// Perform manual iteration...
var v;
while ( true ) {
v = it.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">

* * *

## See Also

- <span class="package-name">[`@stdlib/iter/cuany-by`][@stdlib/iter/cuany-by]</span><span class="delimiter">: </span><span class="description">create an iterator which iteratively applies a test function to iterated values, returning `true` if a test is passed and `false` otherwise.</span>
- <span class="package-name">[`@stdlib/iter/cuevery-by`][@stdlib/iter/cuevery-by]</span><span class="delimiter">: </span><span class="description">create an iterator which iteratively applies a test function to iterated values, returning `false` if a test fails and `true` otherwise.</span>
- <span class="package-name">[`@stdlib/iter/for-each`][@stdlib/iter/for-each]</span><span class="delimiter">: </span><span class="description">create an iterator which invokes a function for each iterated value before returning the iterated value.</span>
- <span class="package-name">[`@stdlib/iter/cunone-by`][@stdlib/iter/cunone-by]</span><span class="delimiter">: </span><span class="description">create an iterator which iteratively applies a test function to iterated values, returning `true` if all values fail and `false` otherwise.</span>
- <span class="package-name">[`@stdlib/iter/cusome`][@stdlib/iter/cusome]</span><span class="delimiter">: </span><span class="description">create an iterator which iteratively checks whether at least `n` iterated values are truthy.</span>

</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">

[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol

<!-- <related-links> -->

[@stdlib/iter/cuany-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/iter/cuany-by

[@stdlib/iter/cuevery-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/iter/cuevery-by

[@stdlib/iter/for-each]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/iter/for-each

[@stdlib/iter/cunone-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/iter/cunone-by

[@stdlib/iter/cusome]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/iter/cusome

<!-- </related-links> -->

</section>

<!-- /.links -->
```
130 changes: 130 additions & 0 deletions lib/node_modules/@stdlib/iter/cusome-by/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* @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 isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
var pkg = require( './../package.json' ).name;
var iterCuSomeBy = 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
};
}
i = -1; // reset index
return {
'done': true
};
}

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


// MAIN //

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

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
iter = iterCuSomeBy( arr, 2, predicate );
if ( typeof iter !== 'object' ) {
b.fail( 'should return an object' );
}
arr.reset();
}
b.toc();
if ( !isIteratorLike( iter ) ) {
b.fail( 'should return an iterator' );
}
b.pass( 'benchmark finished' );
b.end();

function predicate( v ) {
return ( v > 0 );
}
});

bench( pkg+'::loop', function benchmark( b ) {
var values;
var count;
var arr;
var i;
var j;

values = [ 0, 0, 0, 1, 1, 1 ];
arr = createIterator( values );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
count = 0;
for ( j = 0; j < values.length; j++ ) {
if ( predicate( arr.next().value ) ) {
count += 1;
}
if ( count >= 2 ) {
break;
}
}
if ( typeof count !== 'number' ) {
b.fail( 'should be a number' );
}
arr.reset();
}
b.toc();
if ( typeof count !== 'number' ) {
b.fail( 'should be a number' );
}
b.pass( 'benchmark finished' );
b.end();

function predicate( v ) {
return ( v > 0 );
}
});
Loading
Loading