Skip to content

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
Merged
Show file tree
Hide file tree
Changes from 3 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 Feb 24, 2024
64c19d4
fixed issues & implementation of predicate functions at all places
bad-in-coding Feb 25, 2024
cc6be33
fixed linting issues
bad-in-coding Feb 27, 2024
2bde74a
Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
bad-in-coding Mar 2, 2024
2e623ce
Update lib/node_modules/@stdlib/array/base/count-if/lib/index.js
bad-in-coding Mar 2, 2024
516f915
Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js
bad-in-coding Mar 2, 2024
e8b8661
Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js
bad-in-coding Mar 2, 2024
a942040
Update lib/node_modules/@stdlib/array/base/count-if/lib/main.js
bad-in-coding Mar 2, 2024
37ae175
improved type declaration for predicate function
Mar 2, 2024
c14e122
Update lib/node_modules/@stdlib/array/base/count-if/README.md
bad-in-coding Mar 2, 2024
310ea3b
Apply suggestions from code review
Planeshifter Mar 2, 2024
a7ecf09
Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
Planeshifter Mar 2, 2024
b88c937
Update lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
Planeshifter Mar 2, 2024
3c1765f
Apply suggestions from code review
Planeshifter Mar 2, 2024
d8f6acb
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter Mar 2, 2024
4abe551
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter Mar 2, 2024
555404e
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter Mar 2, 2024
82cfd6f
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter Mar 2, 2024
8b026b8
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter Mar 2, 2024
e591873
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter Mar 2, 2024
66f8deb
Update lib/node_modules/@stdlib/array/base/count-if/README.md
Planeshifter Mar 2, 2024
9a400f6
Update lib/node_modules/@stdlib/array/base/count-if/examples/index.js
Planeshifter Mar 2, 2024
085e265
Update test.js
Planeshifter Mar 2, 2024
297f300
Update benchmark.length.js
Planeshifter Mar 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions lib/node_modules/@stdlib/array/base/count-if/README.md
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

> Counts 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 -->
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( 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 lib/node_modules/@stdlib/array/base/count-if/docs/repl.txt
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( v ){
return v === 1
} )
2

See Also
--------

46 changes: 46 additions & 0 deletions lib/node_modules/@stdlib/array/base/count-if/docs/types/index.d.ts
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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/array';

/**
* 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 ];
* var predicate = function( v ) {
* return v > this;
* }
* var n = indexed( x, predicate );
* // returns 2
*/
declare function countIf( x: Collection, predicate: ( ...args: Array<any> ) => boolean, thisArg?: any ): number;


// EXPORTS //

export = countIf;
42 changes: 42 additions & 0 deletions lib/node_modules/@stdlib/array/base/count-if/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* @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 countIf from './index';


// TESTS //

// The function returns a number...
{
countIf( [ 1, 2, 3 ], ()=>{ return true } ); // $ExpectType number
}

// The compiler throws an error if the function is provided an argument which is not a collection or the function is not boolean returning...
{
countIf( [ 5 ], function(){ return 1 } ); // $ExpectError
countIf( true ); // $ExpectError
countIf( false, function(){ return false} ); // $ExpectError
countIf( null ); // $ExpectError
countIf( [ {}, {} ], ()=>{} ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
countIf(); // $ExpectError
countIf( [ 1, 2, 3 ], 2 ); // $ExpectError
}
Loading