Skip to content

Commit ecd3518

Browse files
feat: add array/base/count-if
PR-URL: #1372 Closes: #1324 Signed-off-by: Utkarsh <137638507+Ut-the-pro@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent a2ed2a5 commit ecd3518

File tree

10 files changed

+950
-0
lines changed

10 files changed

+950
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
> Count 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+
function predicate( val ) {
51+
return ( val % 2 === 0 );
52+
}
53+
54+
var out = countIf( x, predicate );
55+
// returns 3
56+
```
57+
58+
If a `predicate` function returns a truthy value, the function counts that value.
59+
60+
The `predicate` function is provided three arguments:
61+
62+
- **value**: current array element.
63+
- **index**: current array element index.
64+
- **arr**: input array.
65+
66+
To set the `predicate` function execution context, provide a `thisArg`.
67+
68+
```javascript
69+
var x = [ 1, 2, 3, 4 ];
70+
71+
var context = {
72+
'target': 3
73+
};
74+
75+
function predicate( value ) {
76+
return ( value > this.target );
77+
}
78+
79+
var out = countIf( x, predicate, context );
80+
// returns 1
81+
```
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
88+
89+
<section class="notes">
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<!-- Package usage examples. -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
105+
var countIf = require( '@stdlib/array/base/count-if' );
106+
107+
var x = bernoulli( 100, 0.5, {
108+
'dtype': 'generic'
109+
});
110+
console.log( x );
111+
112+
function predicate( val ) {
113+
return val === 1;
114+
}
115+
var n = countIf( x, predicate );
116+
console.log( n );
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- 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. -->
124+
125+
<section class="references">
126+
127+
</section>
128+
129+
<!-- /.references -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
</section>
136+
137+
<!-- /.related -->
138+
139+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
140+
141+
<section class="links">
142+
143+
</section>
144+
145+
<!-- /.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 predicate( 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( x, predicate[, thisArg] )
3+
Counts the number of elements in an array that satisfy the provided testing
4+
function.
5+
6+
Parameters
7+
----------
8+
x: ArrayLikeObject
9+
Input array.
10+
11+
predicate: Function
12+
Testing function.
13+
14+
thisArg: any (optional)
15+
Execution context.
16+
17+
Returns
18+
-------
19+
out: integer
20+
Number of truthy values for which the testing function evaluates to
21+
true.
22+
23+
Examples
24+
--------
25+
> var out = {{alias}}( [ 0, 1, 1 ], function predicate( v ) {
26+
... return v === 1;
27+
... } )
28+
2
29+
30+
See Also
31+
--------
32+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns a boolean indicating whether an element passes a test.
27+
*
28+
* @returns boolean indicating whether an element passes a test
29+
*/
30+
type Nullary<U> = ( this: U ) => boolean;
31+
32+
/**
33+
* Returns a boolean indicating whether an element passes a test.
34+
*
35+
* @param value - current array element
36+
* @returns boolean indicating whether an element passes a test
37+
*/
38+
type Unary<T, U> = ( this: U, value: T ) => boolean;
39+
40+
/**
41+
* Returns a boolean indicating whether an element passes a test.
42+
*
43+
* @param value - current array element
44+
* @param index - current array element index
45+
* @returns boolean indicating whether an element passes a test
46+
*/
47+
type Binary<T, U> = ( this: U, value: T, index: number ) => boolean;
48+
49+
/**
50+
* Returns a boolean indicating whether an element passes a test.
51+
*
52+
* @param value - current array element
53+
* @param index - current array element index
54+
* @param arr - input array
55+
* @returns boolean indicating whether an element passes a test
56+
*/
57+
type Ternary<T, U> = ( this: U, value: T, index: number, arr: Collection<T> | AccessorArrayLike<T> ) => boolean;
58+
59+
/**
60+
* Returns a boolean indicating whether an element passes a test.
61+
*
62+
* @param value - current array element
63+
* @param index - current array element index
64+
* @param arr - input array
65+
* @returns boolean indicating whether an element passes a test
66+
*/
67+
type Predicate<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;
68+
69+
/**
70+
* Counts the number of truthy values in an array.
71+
*
72+
* @param x - input array
73+
* @param predicate - testing function
74+
* @param thisArg - function context
75+
* @returns number of values for which the provided function evaluates to true
76+
*
77+
* @example
78+
* var x = [ 0, 1, 0, 1 ];
79+
* function predicate( v ) {
80+
* return v > this;
81+
* }
82+
* var n = countIf( x, predicate, 0 );
83+
* // returns 2
84+
*/
85+
declare function countIf<T = unknown, U = unknown>( x: Collection<T> | AccessorArrayLike<T>, predicate: Predicate<T, U>, thisArg?: ThisParameterType<Predicate<T, U>> ): number;
86+
87+
88+
// EXPORTS //
89+
90+
export = countIf;

0 commit comments

Comments
 (0)