Skip to content

Commit d463fab

Browse files
committed
feat: add array/base/none-by
1 parent 85985d4 commit d463fab

File tree

10 files changed

+1030
-0
lines changed

10 files changed

+1030
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
# noneBy
22+
23+
> Test whether all elements in an array fail a test implemented by a predicate 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 noneBy = require( '@stdlib/array/base/none-by' );
41+
```
42+
43+
#### noneBy( x, predicate\[, thisArg] )
44+
45+
Tests whether all elements in an array fail a test implemented by a `predicate` function.
46+
47+
```javascript
48+
function isPositive( value ) {
49+
return ( value > 0 );
50+
}
51+
52+
var x = [ 0, 0, 0, 0 ];
53+
54+
var bool = noneBy( x, isPositive );
55+
// returns true
56+
```
57+
58+
If a `predicate` function returns a truthy value, the function **immediately** returns `false`.
59+
60+
```javascript
61+
function isPositive( value ) {
62+
return ( value > 0 );
63+
}
64+
65+
var x = [ -1, -2, 3, -4 ];
66+
67+
var bool = noneBy( x, isPositive );
68+
// returns false
69+
```
70+
71+
The `predicate` function is provided three arguments:
72+
73+
- **value**: current array element.
74+
- **index**: current array element index.
75+
- **arr**: input array.
76+
77+
To set the `predicate` function execution context, provide a `thisArg`.
78+
79+
```javascript
80+
function predicate( value ) {
81+
this.count += 1;
82+
return ( value < 0 );
83+
}
84+
85+
var x = [ 1, 2, -3, 4 ];
86+
87+
var context = {
88+
'count': 0
89+
};
90+
91+
var bool = noneBy( x, predicate, context );
92+
// returns false
93+
94+
var cnt = context.count;
95+
// returns 3
96+
```
97+
98+
</section>
99+
100+
<!-- /.usage -->
101+
102+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="notes">
105+
106+
## Notes
107+
108+
- The function performs a linear scan and returns immediately upon encountering a truthy return value. Unlike [`Array.prototype.every`][mdn-array-every], when performing a linear scan, the function does **not** skip `undefined` elements.
109+
- If provided an empty array, the function returns `true`.
110+
111+
</section>
112+
113+
<!-- /.notes -->
114+
115+
<!-- Package usage examples. -->
116+
117+
<section class="examples">
118+
119+
## Examples
120+
121+
<!-- eslint no-undef: "error" -->
122+
123+
```javascript
124+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
125+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
126+
var naryFunction = require( '@stdlib/utils/nary-function' );
127+
var noneBy = require( '@stdlib/array/base/none-by' );
128+
129+
var x = bernoulli( 10, 0.1, {
130+
'dtype': 'int8'
131+
} );
132+
// returns <Int8Array>
133+
134+
var out = noneBy( x, naryFunction( isPositiveInteger, 1 ) );
135+
// returns <boolean>
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- 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. -->
143+
144+
<section class="references">
145+
146+
</section>
147+
148+
<!-- /.references -->
149+
150+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
151+
152+
<section class="related">
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="links">
161+
162+
[mdn-array-every]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
163+
164+
</section>
165+
166+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
27+
var zeros = require( '@stdlib/array/base/zeros' );
28+
var pkg = require( './../package.json' ).name;
29+
var noneBy = 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 = zeros( 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 out;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
out = noneBy( x, isPositiveInteger );
58+
if ( typeof out !== 'boolean' ) {
59+
b.fail( 'should return a boolean' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isBoolean( out ) ) {
64+
b.fail( 'should return a boolean' );
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+
92+
f = createBenchmark( len );
93+
bench( pkg+':dtype=generic,len='+len, f );
94+
}
95+
}
96+
97+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( x, predicate[, thisArg] )
3+
Tests whether all elements in an array fail a test implemented by a
4+
predicate function.
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+
The function performs a linear scan and returns immediately upon
13+
encountering a truthy return value.
14+
15+
If provided an empty array, the function returns `true`.
16+
17+
Parameters
18+
----------
19+
x: Array|TypedArray|Object
20+
Input array.
21+
22+
predicate: Function
23+
Predicate function.
24+
25+
thisArg: any (optional)
26+
Execution context.
27+
28+
Returns
29+
-------
30+
bool: boolean
31+
The function returns `true` if the predicate function returns a falsy
32+
value for all elements; otherwise, the function returns `false`.
33+
34+
Examples
35+
--------
36+
> function f( v ) { return ( v > 0 ); };
37+
> var x = [ 0, 0, 0, 0 ];
38+
> var out = {{alias}}( x, f )
39+
true
40+
41+
See Also
42+
--------
43+

0 commit comments

Comments
 (0)