-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add array/base/cunone-by
#2615
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<!-- | ||
@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. | ||
--> | ||
# cunone-by | ||
|
||
> Cumulatively test whether every element in a provided array passes a test implemented by a predicate function. | ||
|
||
## Usage | ||
|
||
```javascript | ||
var cunoneBy = require( '@stdlib/array/base/cunone-by' ); | ||
|
||
cunoneBy( x, predicate ); | ||
|
||
var bernoulli = require( '@stdlib/random/array/bernoulli' ); | ||
var cunoneBy = require( '@stdlib/array/base/cunone-by' ); | ||
|
||
// Create an array of random values: | ||
var x = bernoulli( 10, 0.6 ); | ||
console.log( x ); | ||
|
||
// Define a predicate function: | ||
function predicate( v ) { | ||
return v === 1; | ||
} | ||
|
||
// Cumulatively determine whether values pass the predicate: | ||
var out = cunoneBy( x, predicate ); | ||
console.log( out ); | ||
|
||
In this example, out will contain an array of boolean values indicating whether each element in x equals 1. | ||
|
||
This module is part of the stdlib ecosystem, providing reliable and tested implementations for standard library functions in JavaScript. | ||
|
||
<!-- 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"> | ||
</section> | ||
<!-- /.links --> |
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. The indentation of this file is off. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @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 pow = require( '@stdlib/math/base/special/pow' ); | ||
var isArray = require( '@stdlib/assert/is-array' ); | ||
var filled = require('@stdlib/array/base/filled'); | ||
var pkg = require( './../package.json' ).name; | ||
var cunoneBy = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x = filled( 1.5, len ); | ||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var y; | ||
var v; | ||
var i; | ||
|
||
y = filled( true, len ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = cunoneBy.assign( x, y, 1, 0 ); | ||
if ( typeof v !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( v ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
*/ | ||
function main() { | ||
var len; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
|
||
min = 1; // 10^min | ||
max = 6; // 10^max | ||
|
||
for ( i = min; i <= max; i++ ) { | ||
len = pow( 10, i ); | ||
f = createBenchmark( len ); | ||
bench( pkg+':assign:len='+len, f ); | ||
} | ||
} | ||
|
||
main(); | ||
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. You need to add trailing newlines. Please ensure EditorConfig is installed as discussed in the contributing guidelines. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* @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 isArray = require( '@stdlib/assert/is-array' ); | ||
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var cunoneBy = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
function predicate( v ) { | ||
return v === false; | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var x; | ||
var i; | ||
var v; | ||
|
||
x = [ false, false, true, false, false ]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = cunoneBy( x, predicate ); | ||
if ( typeof v !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( v ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+':assign', function benchmark( b ) { | ||
var out; | ||
var x; | ||
var y; | ||
var i; | ||
|
||
x = [ false, false, true, false, false ]; | ||
out = [ 0, 0, 0, 0, 0 ]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = cunoneBy.assign( x, out, 1, 0, predicate ); | ||
if ( typeof y !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( y ) || y !== out ) { | ||
b.fail( 'should return the output array' ); | ||
} | ||
for ( i = 0; i < y.length; i++ ) { | ||
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 check is unnecessary. Remove. |
||
if ( !isBoolean( y[i] ) ) { | ||
b.fail( 'should only contain boolean values' ); | ||
} | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* @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 pow = require( '@stdlib/math/base/special/pow' ); | ||
var isArray = require( '@stdlib/assert/is-array' ); | ||
var filled = require( '@stdlib/array/base/filled' ); | ||
var pkg = require( './../package.json' ).name; | ||
var cunoneBy = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Predicate function. | ||
* | ||
* @private | ||
* @param {number} v - array element | ||
* @returns {boolean} predicate result | ||
*/ | ||
function predicate( v ) { | ||
return v < 1.0; | ||
} | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x = filled( 1.5, len ); | ||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var v; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = cunoneBy( x, predicate ); | ||
if ( typeof v !== 'object' ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isArray( v ) ) { | ||
b.fail( 'should return an array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
*/ | ||
function main() { | ||
var len; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
|
||
min = 1; // 10^min | ||
max = 6; // 10^max | ||
|
||
for ( i = min; i <= max; i++ ) { | ||
len = pow( 10, i ); | ||
f = createBenchmark( len ); | ||
bench( pkg+':len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
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 README needs to be updated, as it does not match the style and conventions of other similar files in this project. Please update.
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.
I am working on it, actually having some examinations up my sleeve.So preparing for them.