Skip to content

Commit d258f44

Browse files
committed
feat: add array/base/reject
1 parent 1dc8ec6 commit d258f44

File tree

10 files changed

+1264
-0
lines changed

10 files changed

+1264
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
# reject
22+
23+
> Return a shallow copy of an array containing only those elements which 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 reject = require( '@stdlib/array/base/reject' );
41+
```
42+
43+
#### reject( x, predicate\[, thisArg] )
44+
45+
Returns a shallow copy of an array containing only those elements which fail a test implemented by a `predicate` function.
46+
47+
```javascript
48+
function isPositive( value ) {
49+
return ( value > 0 );
50+
}
51+
52+
var x = [ 1, -2, -3, 4 ];
53+
54+
var out = reject( x, isPositive );
55+
// returns [ -2, -3 ]
56+
```
57+
58+
The `predicate` function is provided three arguments:
59+
60+
- **value**: current array element.
61+
- **index**: current array element index.
62+
- **arr**: input array.
63+
64+
To set the `predicate` function execution context, provide a `thisArg`.
65+
66+
```javascript
67+
function predicate( value ) {
68+
this.count += 1;
69+
return ( value > 0 );
70+
}
71+
72+
var x = [ 1, 2, -3, 4 ];
73+
74+
var context = {
75+
'count': 0
76+
};
77+
78+
var out = reject( x, predicate, context );
79+
// returns [ -3 ]
80+
81+
var cnt = context.count;
82+
// returns 4
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="notes">
92+
93+
## Notes
94+
95+
- The function performs a linear scan and **always** returns a generic array.
96+
97+
</section>
98+
99+
<!-- /.notes -->
100+
101+
<!-- Package usage examples. -->
102+
103+
<section class="examples">
104+
105+
## Examples
106+
107+
<!-- eslint no-undef: "error" -->
108+
109+
```javascript
110+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
111+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
112+
var naryFunction = require( '@stdlib/utils/nary-function' );
113+
var reject = require( '@stdlib/array/base/reject' );
114+
115+
var x = discreteUniform( 10, -5, 5, {
116+
'dtype': 'int32'
117+
});
118+
// returns <Int32Array>
119+
120+
var out = reject( x, naryFunction( isPositiveInteger, 1 ) );
121+
// returns <Int32Array>
122+
```
123+
124+
</section>
125+
126+
<!-- /.examples -->
127+
128+
<!-- 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. -->
129+
130+
<section class="references">
131+
132+
</section>
133+
134+
<!-- /.references -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
</section>
149+
150+
<!-- /.links -->
Lines changed: 97 additions & 0 deletions
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 isArray = require( '@stdlib/assert/is-array' );
26+
var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive;
27+
var zeroTo = require( '@stdlib/array/base/zero-to' );
28+
var pkg = require( './../package.json' ).name;
29+
var reject = 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 = zeroTo( 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 = reject( x, isNegativeInteger );
58+
if ( typeof out !== 'object' ) {
59+
b.fail( 'should return an array' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isArray( out ) ) {
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+
92+
f = createBenchmark( len );
93+
bench( pkg+':dtype=generic,len='+len, f );
94+
}
95+
}
96+
97+
main();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( x, predicate[, thisArg] )
3+
Returns a shallow copy of an array containing only those elements which fail
4+
a test implemented by a 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 always returns a generic array.
13+
14+
Parameters
15+
----------
16+
x: Array|TypedArray|Object
17+
Input array.
18+
19+
predicate: Function
20+
Predicate function.
21+
22+
thisArg: any (optional)
23+
Execution context.
24+
25+
Returns
26+
-------
27+
out: Array|TypedArray|Object
28+
Output array.
29+
30+
Examples
31+
--------
32+
> function f( v ) { return ( v > 0 ); };
33+
> var x = [ 1, -2, -3, 4 ];
34+
> var out = {{alias}}( x, f )
35+
[ -2, -3 ]
36+
37+
See Also
38+
--------
39+

0 commit comments

Comments
 (0)