Skip to content

Commit 48101d5

Browse files
committed
added array/base/count-if
1 parent 31aff93 commit 48101d5

File tree

10 files changed

+831
-0
lines changed

10 files changed

+831
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# countIf
22+
23+
> Counts the number of elements in an array that satisfy the provided testing function.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var countIf = require( '@stdlib/array/base/count-if' );
41+
```
42+
43+
#### countIf( x, predicate[, thisArg] )
44+
45+
Counts the number of elements in an array that satisfy the provided testing function.
46+
47+
```javascript
48+
var x = [ 0, 1, 0, 1, 2 ];
49+
50+
var predicate = function isEven( value ){ return ( value % 2 === 0 ) };
51+
52+
var out = countIf( x, predicate );
53+
// returns 3
54+
```
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
61+
62+
<section class="notes">
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<!-- Package usage examples. -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
78+
var countIf = require( '@stdlib/array/base/count-if' );
79+
80+
var x = bernoulli( 100, 0.5, {
81+
'dtype': 'generic'
82+
});
83+
console.log( x );
84+
85+
var predicate = function ( val ) { return val === 1 }
86+
87+
var n = countIf( x, predicate );
88+
console.log( n );
89+
```
90+
91+
</section>
92+
93+
<!-- /.examples -->
94+
95+
<!-- 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. -->
96+
97+
<section class="references">
98+
99+
</section>
100+
101+
<!-- /.references -->
102+
103+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
104+
105+
<section class="related">
106+
107+
</section>
108+
109+
<!-- /.related -->
110+
111+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
112+
113+
<section class="links">
114+
115+
</section>
116+
117+
<!-- /.links -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
26+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
27+
var pkg = require( './../package.json' ).name;
28+
var countIf = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = bernoulli( len, 0.5, {
42+
'dtype': 'generic'
43+
});
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var out;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = countIf( x, function( v ) {
59+
return v === 1;
60+
} );
61+
if ( typeof out !== 'number' ) {
62+
b.fail( 'should return a number' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isNonNegativeInteger( out ) ) {
67+
b.fail( 'should return a nonnegative integer' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
/**
78+
* Main execution sequence.
79+
*
80+
* @private
81+
*/
82+
function main() {
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
95+
f = createBenchmark( len );
96+
bench( pkg+':dtype=generic,len='+len, f );
97+
}
98+
}
99+
100+
main();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x, predicate[, thisArg] )
3+
Counts the number of elements in an array that satisfy the provided testing function.
4+
5+
Parameters
6+
----------
7+
x: ArrayLikeObject
8+
Input array.
9+
10+
predicate: Function
11+
Testing function.
12+
13+
thisArg: any (optional)
14+
Input function's context.
15+
16+
Returns
17+
-------
18+
out: integer
19+
Number of truthy values for which the testing function evaluates to true.
20+
21+
Examples
22+
--------
23+
> var out = {{alias}}( [ 0, 1, 1 ], function( v ){ return v === 1 } )
24+
2
25+
26+
See Also
27+
--------
28+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Counts the number of truthy values in an array.
27+
*
28+
* @param { Collection } x - input array
29+
* @param { ( ...args: any[] ) => boolean } predicate - testing function
30+
* @param { * } [ thisArg ] - function context
31+
* @returns number of values for which the provided function evaluates to true
32+
*
33+
* @example
34+
* var x = [ 0, 1, 0, 1 ];
35+
* var predicate = function( v, t ){ return v > t; }
36+
* var n = indexed( x, predicate, 0 );
37+
* // returns 2
38+
*/
39+
declare function countIf( x: Collection, predicate: ( ...args: any[] ) => boolean, thisArg?: any ): number;
40+
41+
42+
// EXPORTS //
43+
44+
export = countIf;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import countIf from './index';
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
countIf( [ 1, 2, 3 ], ()=>{ return true } ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided an argument which is not a collection or the function is not boolean returning...
30+
{
31+
countIf( [ 5 ], function(){ return 1 } ); // $ExpectError
32+
countIf( true ); // $ExpectError
33+
countIf( false, function(){ return false} ); // $ExpectError
34+
countIf( null ); // $ExpectError
35+
countIf( [ {}, {} ], ()=>{} ); // $ExpectError
36+
}
37+
38+
// The compiler throws an error if the function is provided an unsupported number of arguments...
39+
{
40+
countIf(); // $ExpectError
41+
countIf( [ 1, 2, 3 ], 2 ); // $ExpectError
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
22+
var countIf = require( './../lib' );
23+
24+
var x = bernoulli( 100, 0.5, {
25+
'dtype': 'generic'
26+
});
27+
console.log( x );
28+
29+
var big = 10;
30+
31+
var predicate = function isBig( val, big ) { return (val > big) }
32+
33+
var n = countIf( x, predicate, big );
34+
console.log( n );

0 commit comments

Comments
 (0)