-
-
Notifications
You must be signed in to change notification settings - Fork 836
feat: add array/base/count-if
#1372
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
Merged
Planeshifter
merged 24 commits into
stdlib-js:develop
from
bad-in-coding:feature/array/base/count-if
Mar 2, 2024
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e3f73ac
added array/base/count-if
bad-in-coding 64c19d4
fixed issues & implementation of predicate functions at all places
bad-in-coding cc6be33
fixed linting issues
bad-in-coding 2bde74a
Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
bad-in-coding 2e623ce
Update lib/node_modules/@stdlib/array/base/count-if/lib/index.js
bad-in-coding 516f915
Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js
bad-in-coding e8b8661
Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js
bad-in-coding a942040
Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js
bad-in-coding 37ae175
improved type declaration for predicate function
c14e122
Update lib/node_modules/@stdlib/array/base/count-if/README.md
bad-in-coding 310ea3b
Apply suggestions from code review
Planeshifter a7ecf09
Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
Planeshifter b88c937
Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
Planeshifter 3c1765f
Apply suggestions from code review
Planeshifter d8f6acb
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter 4abe551
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter 555404e
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter 82cfd6f
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter 8b026b8
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter e591873
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter 66f8deb
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter 9a400f6
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter 085e265
Update test.js
Planeshifter 297f300
Update benchmark.length.js
Planeshifter 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 hidden or 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,145 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# countIf | ||
|
||
> Count the number of elements in an array that satisfy the provided testing 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 countIf = require( '@stdlib/array/base/count-if' ); | ||
``` | ||
|
||
#### countIf( x, predicate\[, thisArg] ) | ||
|
||
Counts the number of elements in an array that satisfy the provided testing function. | ||
|
||
```javascript | ||
var x = [ 0, 1, 0, 1, 2 ]; | ||
|
||
function predicate( val ) { | ||
return ( val % 2 === 0 ); | ||
} | ||
|
||
var out = countIf( x, predicate ); | ||
// returns 3 | ||
``` | ||
|
||
If a `predicate` function returns a truthy value, the function counts that value. | ||
|
||
The `predicate` function is provided three arguments: | ||
|
||
- **value**: current array element. | ||
- **index**: current array element index. | ||
- **arr**: input array. | ||
|
||
To set the `predicate` function execution context, provide a `thisArg`. | ||
|
||
```javascript | ||
var x = [ 1, 2, 3, 4 ]; | ||
|
||
var context = { | ||
'target': 3 | ||
}; | ||
|
||
function predicate( value ) { | ||
return ( value > this.target ); | ||
} | ||
|
||
var out = countIf( x, predicate, context ); | ||
// returns 1 | ||
``` | ||
|
||
</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"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var bernoulli = require( '@stdlib/random/array/bernoulli' ); | ||
var countIf = require( '@stdlib/array/base/count-if' ); | ||
|
||
var x = bernoulli( 100, 0.5, { | ||
'dtype': 'generic' | ||
}); | ||
console.log( x ); | ||
|
||
function predicate( val ) { | ||
return val === 1; | ||
} | ||
var n = countIf( x, predicate ); | ||
console.log( n ); | ||
``` | ||
|
||
</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"> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
100 changes: 100 additions & 0 deletions
100
lib/node_modules/@stdlib/array/base/count-if/benchmark/benchmark.length.js
This file contains hidden or 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,100 @@ | ||
/** | ||
* @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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; | ||
var bernoulli = require( '@stdlib/random/array/bernoulli' ); | ||
var pkg = require( './../package.json' ).name; | ||
var countIf = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x = bernoulli( len, 0.5, { | ||
'dtype': 'generic' | ||
}); | ||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var out; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = countIf( x, function predicate( v ) { | ||
return v === 1; | ||
} ); | ||
if ( typeof out !== 'number' ) { | ||
b.fail( 'should return a number' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isNonNegativeInteger( out ) ) { | ||
b.fail( 'should return a nonnegative integer' ); | ||
} | ||
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+':dtype=generic,len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
This file contains hidden or 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,32 @@ | ||
|
||
{{alias}}( x, predicate[, thisArg] ) | ||
Counts the number of elements in an array that satisfy the provided testing | ||
function. | ||
|
||
Parameters | ||
---------- | ||
x: ArrayLikeObject | ||
Input array. | ||
|
||
predicate: Function | ||
Testing function. | ||
|
||
thisArg: any (optional) | ||
Execution context. | ||
|
||
Returns | ||
------- | ||
out: integer | ||
Number of truthy values for which the testing function evaluates to | ||
true. | ||
|
||
Examples | ||
-------- | ||
> var out = {{alias}}( [ 0, 1, 1 ], function predicate( v ) { | ||
... return v === 1; | ||
... } ) | ||
2 | ||
|
||
See Also | ||
-------- | ||
|
90 changes: 90 additions & 0 deletions
90
lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts
This file contains hidden or 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,90 @@ | ||
/* | ||
* @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 { Collection, AccessorArrayLike } from '@stdlib/types/array'; | ||
|
||
/** | ||
* Returns a boolean indicating whether an element passes a test. | ||
* | ||
* @returns boolean indicating whether an element passes a test | ||
*/ | ||
type Nullary<U> = ( this: U ) => boolean; | ||
|
||
/** | ||
* Returns a boolean indicating whether an element passes a test. | ||
* | ||
* @param value - current array element | ||
* @returns boolean indicating whether an element passes a test | ||
*/ | ||
type Unary<T, U> = ( this: U, value: T ) => boolean; | ||
|
||
/** | ||
* Returns a boolean indicating whether an element passes a test. | ||
* | ||
* @param value - current array element | ||
* @param index - current array element index | ||
* @returns boolean indicating whether an element passes a test | ||
*/ | ||
type Binary<T, U> = ( this: U, value: T, index: number ) => boolean; | ||
|
||
/** | ||
* Returns a boolean indicating whether an element passes a test. | ||
* | ||
* @param value - current array element | ||
* @param index - current array element index | ||
* @param arr - input array | ||
* @returns boolean indicating whether an element passes a test | ||
*/ | ||
type Ternary<T, U> = ( this: U, value: T, index: number, arr: Collection<T> | AccessorArrayLike<T> ) => boolean; | ||
|
||
/** | ||
* Returns a boolean indicating whether an element passes a test. | ||
* | ||
* @param value - current array element | ||
* @param index - current array element index | ||
* @param arr - input array | ||
* @returns boolean indicating whether an element passes a test | ||
*/ | ||
type Predicate<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>; | ||
|
||
/** | ||
* Counts the number of truthy values in an array. | ||
* | ||
* @param x - input array | ||
* @param predicate - testing function | ||
* @param thisArg - function context | ||
* @returns number of values for which the provided function evaluates to true | ||
* | ||
* @example | ||
* var x = [ 0, 1, 0, 1 ]; | ||
* function predicate( v ) { | ||
* return v > this; | ||
* } | ||
* var n = countIf( x, predicate, 0 ); | ||
* // returns 2 | ||
*/ | ||
declare function countIf<T = unknown, U = unknown>( x: Collection<T> | AccessorArrayLike<T>, predicate: Predicate<T, U>, thisArg?: ThisParameterType<Predicate<T, U>> ): number; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = countIf; |
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.
Uh oh!
There was an error while loading. Please reload this page.