Skip to content

Commit f8fcc76

Browse files
committed
feat: add array/base/count-ifs
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent d998cc3 commit f8fcc76

File tree

10 files changed

+1026
-0
lines changed

10 files changed

+1026
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# countIfs
22+
23+
> Count the number of elements in the input arrays which pass the tests implemented by the predicate functions.
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 countIfs = require( '@stdlib/array/base/count-ifs' );
41+
```
42+
43+
#### countIfs( x0, predicate0\[, x1, predicate1\[, x2, predicate2, ...]] )
44+
45+
Counts the number of elements in the input arrays which pass the tests implemented by the predicate functions.
46+
47+
```javascript
48+
function predicate0( value ) {
49+
return ( value > 0 );
50+
}
51+
52+
function predicate1( value ) {
53+
return ( value < 3 );
54+
}
55+
56+
var x0 = [ 0, 1, 0, 1, 2 ];
57+
var x1 = [ 2, 3, 1, 2, 5 ];
58+
59+
var out = countIfs( x0, predicate0, x1, predicate1 );
60+
// returns 1
61+
```
62+
63+
The `predicate` function is provided three arguments:
64+
65+
- **value**: current array element.
66+
- **index**: current array element index.
67+
- **arr**: input array.
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- The function assumes that all input arrays have the same length.
80+
- The function supports array-like objects supporting the accessor protocol (e.g., [`Complex128Array`][@stdlib/array/complex128], [`Complex64Array`][@stdlib/array/complex64], etc).
81+
82+
</section>
83+
84+
<!-- /.notes -->
85+
86+
<!-- Package usage examples. -->
87+
88+
<section class="examples">
89+
90+
## Examples
91+
92+
<!-- eslint no-undef: "error" -->
93+
94+
```javascript
95+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
96+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
97+
var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive;
98+
var naryFunction = require( '@stdlib/utils/nary-function' );
99+
var countIfs = require( '@stdlib/array/base/count-ifs' );
100+
101+
var x = discreteUniform( 10, -5, 5, {
102+
'dtype': 'int32'
103+
});
104+
// returns <Int32Array>
105+
106+
var y = discreteUniform( 10, -5, 5, {
107+
'dtype': 'int32'
108+
});
109+
// returns <Int32Array>
110+
111+
var out = countIfs( x, naryFunction( isPositiveInteger, 1 ), y, naryFunction( isNegativeInteger, 1 ) ); // eslint-disable-line max-len
112+
// returns <number>
113+
114+
console.log( x );
115+
console.log( y );
116+
console.log( out );
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+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
144+
145+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
146+
147+
</section>
148+
149+
<!-- /.links -->
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 countIfs = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* First predicate function.
35+
*
36+
* @private
37+
* @param {number} v - value
38+
* @returns {boolean} result
39+
*/
40+
function predicate0( v ) {
41+
return v > 0.0;
42+
}
43+
44+
/**
45+
* Second predicate function.
46+
*
47+
* @private
48+
* @param {number} v - value
49+
* @returns {boolean} result
50+
*/
51+
function predicate1( v ) {
52+
return v < 0.0;
53+
}
54+
55+
/**
56+
* Creates a benchmark function.
57+
*
58+
* @private
59+
* @param {PositiveInteger} len - array length
60+
* @returns {Function} benchmark function
61+
*/
62+
function createBenchmark( len ) {
63+
var x0 = bernoulli( len, 0.5, {
64+
'dtype': 'generic'
65+
});
66+
var x1 = bernoulli( len, 0.5, {
67+
'dtype': 'generic'
68+
});
69+
return benchmark;
70+
71+
/**
72+
* Benchmark function.
73+
*
74+
* @private
75+
* @param {Benchmark} b - benchmark instance
76+
*/
77+
function benchmark( b ) {
78+
var out;
79+
var i;
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
out = countIfs( x0, predicate0, x1, predicate1 );
84+
if ( typeof out !== 'number' ) {
85+
b.fail( 'should return a number' );
86+
}
87+
}
88+
b.toc();
89+
if ( !isNonNegativeInteger( out ) ) {
90+
b.fail( 'should return a nonnegative integer' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
}
95+
}
96+
97+
98+
// MAIN //
99+
100+
/**
101+
* Main execution sequence.
102+
*
103+
* @private
104+
*/
105+
function main() {
106+
var len;
107+
var min;
108+
var max;
109+
var f;
110+
var i;
111+
112+
min = 1; // 10^min
113+
max = 6; // 10^max
114+
115+
for ( i = min; i <= max; i++ ) {
116+
len = pow( 10, i );
117+
118+
f = createBenchmark( len );
119+
bench( pkg+':dtype=generic,len='+len, f );
120+
}
121+
}
122+
123+
main();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( x0, predicate0[, x1, predicate1[, x2, predicate2, ...]] )
3+
Counts the number of elements in the input arrays which pass the tests
4+
implemented by the predicate functions.
5+
6+
The predicate function is provided three arguments:
7+
8+
- value: current array element.
9+
- index: current array element index.
10+
- arr: the input array.
11+
12+
Parameters
13+
----------
14+
x0: ArrayLikeObject
15+
First input array.
16+
17+
predicate0: Function
18+
First predicate function.
19+
20+
x1: ArrayLikeObject (optional)
21+
Second input array.
22+
23+
predicate1: Function (optional)
24+
Second predicate function.
25+
26+
Returns
27+
-------
28+
out: integer
29+
Result.
30+
31+
Examples
32+
--------
33+
> function f1( v ) { return ( v > 0 ); };
34+
> function f2( v ) { return ( v < 0 ); };
35+
> var out = {{alias}}( [ 0, 1, 1 ], f1, [ 1, -1, -1 ], f2 )
36+
2
37+
38+
See Also
39+
--------

0 commit comments

Comments
 (0)