Skip to content

Commit 24e9908

Browse files
Kaif987Planeshifterkgryte
authored
feat: add array/base/cunone-by package
PR-URL: #2835 Closes: #2325 --------- Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent edcea47 commit 24e9908

File tree

15 files changed

+1733
-0
lines changed

15 files changed

+1733
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
2+
<!--
3+
4+
@license Apache-2.0
5+
6+
Copyright (c) 2024 The Stdlib Authors.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
20+
-->
21+
22+
# cunoneBy
23+
24+
> Cumulatively test whether every array element in a provided array fails a test implemented by a predicate function.
25+
26+
<section class="usage">
27+
28+
## Usage
29+
30+
```javascript
31+
var cunoneBy = require( '@stdlib/array/base/cunone-by' );
32+
```
33+
34+
#### cunoneBy( x, predicate\[, thisArg ] )
35+
36+
Cumulatively tests whether every array element in a provided array fails a test implemented by a predicate function.
37+
38+
```javascript
39+
function fcn( value) {
40+
return ( value > 0 );
41+
}
42+
43+
var x = [ 0, 0, 0, 1, 0 ];
44+
45+
var y = cunoneBy( x, fcn );
46+
// returns [ true, true, true, false, false ]
47+
```
48+
49+
The invoked `predicate` function is provided three arguments:
50+
51+
- **value**: collection element.
52+
- **index**: collection index.
53+
- **collection**: input collection.
54+
55+
To set the function execution context, provide a `thisArg`.
56+
57+
```javascript
58+
function fcn( v ) {
59+
this.count += 1;
60+
return ( v > 0 );
61+
}
62+
63+
var x = [ 0, 0, 1, 0, 0 ];
64+
65+
var context = {
66+
'count': 0
67+
};
68+
69+
var bool = cunoneBy( x, fcn, context );
70+
// returns [ true, true, false, false, false ]
71+
72+
var count = context.count;
73+
// returns 3
74+
```
75+
76+
#### cunoneBy.assign( x, out, stride, offset, predicate\[, thisArg ] )
77+
78+
Cumulatively tests whether every array element in a provided array fails a test implemented by a predicate function and assigns the values to elements in a provided output array.
79+
80+
```javascript
81+
function fcn( v ) {
82+
return ( v > 0 );
83+
}
84+
85+
var x = [ 0, 0, 0, 1, 0 ];
86+
var y = [ false, null, false, null, false, null, false, null, false, null ];
87+
88+
var out = cunoneBy.assign( x, y, 2, 0, fcn );
89+
// returns [ true, null, true, null, true, null, false, null, false, null ]
90+
91+
var bool = ( out === y );
92+
// returns true
93+
```
94+
95+
</section>
96+
97+
<!-- /.usage -->
98+
99+
<section class="notes">
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
113+
var cunoneBy = require( '@stdlib/array/base/cunone-by' );
114+
115+
function fcn( value ) {
116+
return ( value > 0 );
117+
}
118+
119+
// Create an array of random values:
120+
var x = bernoulli( 10, 0.1 );
121+
console.log( x );
122+
123+
// Cumulatively tests whether every array element fails a test:
124+
var y = cunoneBy( x, fcn );
125+
console.log( y );
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
</section>
145+
146+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
27+
var filled = require( '@stdlib/array/base/filled' );
28+
var pkg = require( './../package.json' ).name;
29+
var cunoneBy = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = filled( 0, len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var y;
53+
var v;
54+
var i;
55+
56+
y = filled( false, len );
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
v = cunoneBy.assign( x, y, 1, 0, isPositiveInteger );
61+
if ( typeof v !== 'object' ) {
62+
b.fail( 'should return an array' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isArray( v ) ) {
67+
b.fail( 'should return an array' );
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+
f = createBenchmark( len );
95+
bench( pkg+':assign:len='+len, f );
96+
}
97+
}
98+
99+
main();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var cunoneBy = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::copy:len=100', function benchmark( b ) {
34+
var x;
35+
var i;
36+
var v;
37+
38+
x = zeroTo( 100 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
v = cunoneBy( x, isPositiveInteger );
43+
if ( typeof v !== 'object' ) {
44+
b.fail( 'should return an array' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isArray( v ) ) {
49+
b.fail( 'should return an array' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
27+
var filled = require( '@stdlib/array/base/filled' );
28+
var pkg = require( './../package.json' ).name;
29+
var cunoneBy = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = filled( 0, len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var v;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = cunoneBy( x, isPositiveInteger );
58+
if ( typeof v !== 'object' ) {
59+
b.fail( 'should return an array' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isArray( v ) ) {
64+
b.fail( 'should return an array' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();

0 commit comments

Comments
 (0)