Skip to content

Commit d9ef2aa

Browse files
feat: add array/base/cuany
PR-URL: #2375 Ref: #2319 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cba0d92 commit d9ef2aa

File tree

15 files changed

+1615
-0
lines changed

15 files changed

+1615
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
# cuany
22+
23+
> Cumulatively test whether at least one element in a provided array is truthy.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var cuany = require( '@stdlib/array/base/cuany' );
31+
```
32+
33+
#### cuany( x )
34+
35+
Cumulatively tests whether at least one element in a provided array is truthy.
36+
37+
```javascript
38+
var x = [ false, false, true, false, false ];
39+
40+
var y = cuany( x );
41+
// returns [ false, false, true, true, true ];
42+
```
43+
44+
#### cuany.assign( x, out, stride, offset )
45+
46+
Cumulatively tests whether at least one element in a provided array is truthy and assigns results to a provided output array.
47+
48+
```javascript
49+
var x = [ false, false, true, false, false ];
50+
var y = [ false, null, false, null, false, null, false, null, false, null ];
51+
52+
var out = cuany.assign( x, y, 2, 0 );
53+
// returns [ false, null, false, null, true, null, true, null, true, null ]
54+
55+
var bool = ( out === y );
56+
// returns true
57+
```
58+
59+
The function supports the following parameters:
60+
61+
- **x**: input array.
62+
- **out**: output array.
63+
- **stride**: output array stride.
64+
- **offset**: output array offset.
65+
66+
</section>
67+
68+
<!-- /.usage -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
84+
var cuany = require( '@stdlib/array/base/cuany' );
85+
86+
// Create an array of random values:
87+
var x = bernoulli( 10, 0.1 );
88+
console.log( x );
89+
90+
// Cumulatively determine whether values are truthy:
91+
var out = cuany( x );
92+
console.log( out );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
</section>
104+
105+
<!-- /.related -->
106+
107+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="links">
110+
111+
112+
</section>
113+
114+
<!-- /.links -->
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 isArray = require( '@stdlib/assert/is-array' );
26+
var filled = require('@stdlib/array/base/filled');
27+
var pkg = require( './../package.json' ).name;
28+
var cuany = 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 = filled( 1.5, len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var y;
52+
var v;
53+
var i;
54+
55+
y = filled( false, len );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = cuany.assign( x, y, 1, 0 );
60+
if ( typeof v !== 'object' ) {
61+
b.fail( 'should return an array' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isArray( v ) ) {
66+
b.fail( 'should return an array' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
f = createBenchmark( len );
94+
bench( pkg+':assign:len='+len, f );
95+
}
96+
}
97+
98+
main();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 isArray = require( '@stdlib/assert/is-array' );
25+
var pkg = require( './../package.json' ).name;
26+
var cuany = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var x;
33+
var i;
34+
var v;
35+
36+
x = [ false, false, true, false, false ];
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
v = cuany( x );
41+
if ( typeof v !== 'object' ) {
42+
b.fail( 'should return an array' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isArray( v ) ) {
47+
b.fail( 'should return an array' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 isArray = require( '@stdlib/assert/is-array' );
26+
var filled = require('@stdlib/array/base/filled');
27+
var pkg = require( './../package.json' ).name;
28+
var cuany = 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 = filled( 1.5, len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = cuany( x );
57+
if ( typeof v !== 'object' ) {
58+
b.fail( 'should return an array' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isArray( v ) ) {
63+
b.fail( 'should return an array' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':len='+len, f );
92+
}
93+
}
94+
95+
main();

0 commit comments

Comments
 (0)