Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Apr 7, 2024
1 parent 5bcb35d commit 76a8a6e
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 254 deletions.
3 changes: 3 additions & 0 deletions base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ The namespace exports the following:
- <span class="signature">[`unary5d( arrays, shape, fcn )`][@stdlib/array/base/unary5d]</span><span class="delimiter">: </span><span class="description">apply a unary callback to elements in a five-dimensional nested input array and assign results to elements in a five-dimensional nested output array.</span>
- <span class="signature">[`unarynd( arrays, shape, fcn )`][@stdlib/array/base/unarynd]</span><span class="delimiter">: </span><span class="description">apply a unary callback to elements in an n-dimensional nested input array and assign results to elements in an n-dimensional nested output array.</span>
- <span class="signature">[`unitspace( start, stop )`][@stdlib/array/base/unitspace]</span><span class="delimiter">: </span><span class="description">generate a linearly spaced numeric array whose elements increment by 1.</span>
- <span class="signature">[`arrayWith( x, index, value )`][@stdlib/array/base/with]</span><span class="delimiter">: </span><span class="description">return a new array with the element at the specified index replaced with a provided value.</span>
- <span class="signature">[`zeroTo( n )`][@stdlib/array/base/zero-to]</span><span class="delimiter">: </span><span class="description">generate a linearly spaced numeric array whose elements increment by 1 starting from zero.</span>
- <span class="signature">[`zeros( len )`][@stdlib/array/base/zeros]</span><span class="delimiter">: </span><span class="description">create a zero-filled "generic" array.</span>
- <span class="signature">[`zeros2d( shape )`][@stdlib/array/base/zeros2d]</span><span class="delimiter">: </span><span class="description">create a zero-filled two-dimensional nested array.</span>
Expand Down Expand Up @@ -565,6 +566,8 @@ console.log( objectKeys( ns ) );

[@stdlib/array/base/unitspace]: https://github.com/stdlib-js/array/tree/main/base/unitspace

[@stdlib/array/base/with]: https://github.com/stdlib-js/array/tree/main/base/with

[@stdlib/array/base/zero-to]: https://github.com/stdlib-js/array/tree/main/base/zero-to

[@stdlib/array/base/zeros]: https://github.com/stdlib-js/array/tree/main/base/zeros
Expand Down
52 changes: 28 additions & 24 deletions base/count-if/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.

# countIf

> Count the number of elements in an array that satisfy the provided testing function.
> Count the number of elements in an array which 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. -->

Expand All @@ -42,21 +42,19 @@ 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.
Counts the number of elements in an array which pass a test implemented by a predicate function.

```javascript
var x = [ 0, 1, 0, 1, 2 ];

function predicate( val ) {
return ( val % 2 === 0 );
function predicate( value ) {
return ( value > 0 );
}

var x = [ 0, 1, 0, 1, 2 ];

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.
Expand All @@ -66,18 +64,22 @@ The `predicate` function is provided three arguments:
To set the `predicate` function execution context, provide a `thisArg`.

```javascript
var x = [ 1, 2, 3, 4 ];
function predicate( value ) {
this.count += 1;
return ( value > 0 );
}

var x = [ 0, 1, 0, 1, 2 ];

var context = {
'target': 3
'count': 0
};

function predicate( value ) {
return ( value > this.target );
}

var out = countIf( x, predicate, context );
// returns 1
// returns 3

var cnt = context.count;
// returns 5
```

</section>
Expand All @@ -101,19 +103,21 @@ var out = countIf( x, predicate, context );
<!-- eslint no-undef: "error" -->

```javascript
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
var naryFunction = require( '@stdlib/utils/nary-function' );
var countIf = require( '@stdlib/array/base/count-if' );

var x = bernoulli( 100, 0.5, {
'dtype': 'generic'
var x = discreteUniform( 10, -5, 5, {
'dtype': 'int32'
});
console.log( x );
// returns <Int32Array>

function predicate( val ) {
return val === 1;
}
var n = countIf( x, predicate );
console.log( n );
var out = countIf( x, naryFunction( isPositiveInteger, 1 ) );
// returns <number>

console.log( x );
console.log( out );
```

</section>
Expand Down
15 changes: 12 additions & 3 deletions base/count-if/benchmark/benchmark.length.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ var countIf = require( './../lib' );

// FUNCTIONS //

/**
* Predicate function.
*
* @private
* @param {number} v - value
* @returns {boolean} result
*/
function predicate( v ) {
return v > 0.0;
}

/**
* Creates a benchmark function.
*
Expand All @@ -55,9 +66,7 @@ function createBenchmark( len ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = countIf( x, function predicate( v ) {
return v === 1;
} );
out = countIf( x, predicate );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
Expand Down
20 changes: 12 additions & 8 deletions base/count-if/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@

{{alias}}( x, predicate[, thisArg] )
Counts the number of elements in an array that satisfy the provided testing
function.
Counts the number of elements in an array which pass a test implemented by a
predicate function.

The predicate function is provided three arguments:

- value: current array element.
- index: current array element index.
- arr: the input array.

Parameters
----------
x: ArrayLikeObject
Input array.

predicate: Function
Testing function.
Predicate function.

thisArg: any (optional)
Execution context.

Returns
-------
out: integer
Number of truthy values for which the testing function evaluates to
true.
Result.

Examples
--------
> var out = {{alias}}( [ 0, 1, 1 ], function predicate( v ) {
... return v === 1;
... } )
> function f( v ) { return ( v > 0 ); };
> var out = {{alias}}( [ 0, 1, 1 ], f )
2

See Also
Expand Down
18 changes: 10 additions & 8 deletions base/count-if/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,22 @@ type Ternary<T, U> = ( this: U, value: T, index: number, arr: Collection<T> | Ac
type Predicate<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;

/**
* Counts the number of truthy values in an array.
* Counts the number of elements in an array which pass a test implemented by a predicate function.
*
* @param x - input array
* @param predicate - testing function
* @param thisArg - function context
* @returns number of values for which the provided function evaluates to true
* @param predicate - predicate function
* @param thisArg - predicate function execution context
* @returns result
*
* @example
* var x = [ 0, 1, 0, 1 ];
* function predicate( v ) {
* return v > this;
* return v > 0;
* }
* var n = countIf( x, predicate, 0 );
* // returns 2
*
* var x = [ 0, 1, 0, 1, 1 ];
*
* var n = countIf( x, predicate );
* // returns 3
*/
declare function countIf<T = unknown, U = unknown>( x: Collection<T> | AccessorArrayLike<T>, predicate: Predicate<T, U>, thisArg?: ThisParameterType<Predicate<T, U>> ): number;

Expand Down
77 changes: 68 additions & 9 deletions base/count-if/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,86 @@
* limitations under the License.
*/

import countIf from './index';
import toAccessorArray = require( './../../../../base/to-accessor-array' );
import countIf = require( './index' );

/**
* Tests whether a value is positive.
*
* @param value - input value
* @returns boolean indicating whether an element is positive
*/
function isPositive( value: number ): boolean {
return ( value > 0 );
}


// TESTS //

// The function returns a number...
{
countIf( [ 1, 2, 3 ], () => { return true } ); // $ExpectType number
countIf( [ 1, 2, 3 ], isPositive ); // $ExpectType number
countIf( new Float64Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Float32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Int32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Int16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Int8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Uint32Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Uint16Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Uint8Array( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number
countIf( toAccessorArray( [ 1, 2, 3 ] ), isPositive ); // $ExpectType number

countIf( [ 1, 2, 3 ], isPositive, {} ); // $ExpectType number
countIf( new Float64Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Float32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Int32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Int16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Int8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Uint32Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Uint16Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Uint8Array( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( new Uint8ClampedArray( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
countIf( toAccessorArray( [ 1, 2, 3 ] ), isPositive, {} ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a collection...
{
countIf( 2, isPositive ); // $ExpectError
countIf( false, isPositive ); // $ExpectError
countIf( true, isPositive ); // $ExpectError
countIf( {}, isPositive ); // $ExpectError

countIf( 2, isPositive, {} ); // $ExpectError
countIf( false, isPositive, {} ); // $ExpectError
countIf( true, isPositive, {} ); // $ExpectError
countIf( {}, isPositive, {} ); // $ExpectError
}

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

countIf( [ 1, 2, 3 ], 'abc', {} ); // $ExpectError
countIf( [ 1, 2, 3 ], 2, {} ); // $ExpectError
countIf( [ 1, 2, 3 ], false, {} ); // $ExpectError
countIf( [ 1, 2, 3 ], true, {} ); // $ExpectError
countIf( [ 1, 2, 3 ], null, {} ); // $ExpectError
countIf( [ 1, 2, 3 ], void 0, {} ); // $ExpectError
countIf( [ 1, 2, 3 ], {}, {} ); // $ExpectError
countIf( [ 1, 2, 3 ], [], {} ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
countIf(); // $ExpectError
countIf( [ 1, 2, 3 ], 2 ); // $ExpectError
countIf( [ 1, 2, 3 ] ); // $ExpectError
countIf( [ 1, 2, 3 ], isPositive, {}, {} ); // $ExpectError
}
21 changes: 10 additions & 11 deletions base/count-if/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@

'use strict';

var bernoulli = require( '@stdlib/random/array/bernoulli' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
var naryFunction = require( '@stdlib/utils/nary-function' );
var countIf = require( './../lib' );

var x = bernoulli( 100, 0.5, {
'dtype': 'generic'
var x = discreteUniform( 10, -5, 5, {
'dtype': 'int32'
});
console.log( x );

var threshold = 0;
// returns <Int32Array>

function predicate( val ) {
return val === 1;
}
var out = countIf( x, naryFunction( isPositiveInteger, 1 ) );
// returns <number>

var n = countIf( x, predicate, threshold );
console.log( n );
console.log( x );
console.log( out );
8 changes: 4 additions & 4 deletions base/count-if/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
'use strict';

/**
* Count the number of elements in an array that satisfy the provided testing function.
* Count the number of elements in an array which pass a test implemented by a predicate function.
*
* @module @stdlib/array/base/count-if
*
* @example
* var countIf = require( '@stdlib/array/base/count-if' );
*
* var x = [ 0, 1, 0, 1, 2 ];
*
* function predicate( value ) {
* return ( value % 2 === 0 )
* return ( value > 0 );
* }
*
* var x = [ 0, 1, 0, 1, 2 ];
*
* var n = countIf( x, predicate );
* // returns 3
*/
Expand Down
Loading

0 comments on commit 76a8a6e

Please sign in to comment.