Skip to content

Commit e7e8166

Browse files
committed
feat: add array/base/mskreject
1 parent 439d442 commit e7e8166

File tree

11 files changed

+697
-0
lines changed

11 files changed

+697
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
# mskreject
22+
23+
> Return a new array by applying a mask to a provided input array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var mskreject = require( '@stdlib/array/base/mskreject' );
31+
```
32+
33+
#### mskreject( x, mask )
34+
35+
Returns a new array by applying a mask to a provided input array.
36+
37+
```javascript
38+
var x = [ 1, 2, 3, 4 ];
39+
40+
var y = mskreject( x, [ 0, 1, 0, 1 ] );
41+
// returns [ 1, 3 ]
42+
```
43+
44+
</section>
45+
46+
<!-- /.usage -->
47+
48+
<section class="notes">
49+
50+
## Notes
51+
52+
- The function **always** returns a new "generic" array.
53+
- If a `mask` array element is falsy, the corresponding element in `x` is **included** in the output array; otherwise, the corresponding element in `x` is "masked" and thus **excluded** from the output array.
54+
55+
</section>
56+
57+
<!-- /.notes -->
58+
59+
<section class="examples">
60+
61+
## Examples
62+
63+
<!-- eslint no-undef: "error" -->
64+
65+
```javascript
66+
var zeroTo = require( '@stdlib/array/base/zero-to' );
67+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
68+
var mskreject = require( '@stdlib/array/base/mskreject' );
69+
70+
// Generate a linearly spaced array:
71+
var x = zeroTo( 20 );
72+
73+
// Generate a random mask:
74+
var mask = bernoulli( x.length, 0.5, {
75+
'dtype': 'generic'
76+
});
77+
78+
// Filter an array using the mask:
79+
var y = mskreject( x, mask );
80+
81+
console.log( x );
82+
console.log( mask );
83+
console.log( y );
84+
```
85+
86+
</section>
87+
88+
<!-- /.examples -->
89+
90+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
91+
92+
<section class="related">
93+
94+
</section>
95+
96+
<!-- /.related -->
97+
98+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
99+
100+
<section class="links">
101+
102+
</section>
103+
104+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 zeros = require( '@stdlib/array/base/zeros' );
27+
var pkg = require( './../package.json' ).name;
28+
var mskreject = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::copy:len=100', function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
var v;
38+
39+
x = zeroTo( 100 );
40+
y = zeros( x.length );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
v = mskreject( x, y );
45+
if ( typeof v !== 'object' ) {
46+
b.fail( 'should return an array' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isArray( v ) ) {
51+
b.fail( 'should return an array' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
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 zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var zeros = require( '@stdlib/array/base/zeros' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var mskreject = 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+
var y = zeros( len );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var v;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = mskreject( x, y );
59+
if ( typeof v !== 'object' ) {
60+
b.fail( 'should return an array' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isArray( v ) ) {
65+
b.fail( 'should return an array' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':len='+len, f );
94+
}
95+
}
96+
97+
main();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, mask )
3+
Returns a new array by applying a mask to a provided input array.
4+
5+
If a mask array element is falsy, the corresponding element in `x` is
6+
included in the output array; otherwise, the corresponding element in `x` is
7+
"masked" and thus excluded from the output array.
8+
9+
Parameters
10+
----------
11+
x: ArrayLikeObject
12+
Input array.
13+
14+
mask: ArrayLikeObject
15+
Mask array.
16+
17+
Returns
18+
-------
19+
out: Array
20+
Output array.
21+
22+
Examples
23+
--------
24+
> var x = [ 1, 2, 3, 4 ];
25+
> var y = {{alias}}( x, [ 0, 1, 0, 1 ] )
26+
[ 1, 3 ]
27+
28+
See Also
29+
--------
30+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns a new array by applying a mask to a provided input array.
27+
*
28+
* @param x - input array
29+
* @param mask - mask array
30+
* @returns output array
31+
*
32+
* @example
33+
* var x = [ 1, 2, 3, 4 ];
34+
*
35+
* var y = mskreject( x, [ 0, 1, 0, 1 ] );
36+
* // returns [ 1, 3 ]
37+
*/
38+
declare function mskreject<T = unknown>( x: Collection<T>, mask: Collection ): Array<T>;
39+
40+
41+
// EXPORTS //
42+
43+
export = mskreject;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
import mskreject = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
mskreject( [ 1, 2, 3, 4 ], [ 0, 0, 0, 0 ] ); // $ExpectType number[]
27+
mskreject<any>( [ 1, 2, 3, 4 ], [ 0, 0, 0, 0 ] ); // $ExpectType any[]
28+
mskreject<number>( [ 1, 2, 3, 4 ], [ 0, 0, 0, 0 ] ); // $ExpectType number[]
29+
mskreject<string>( [ '1', '2', '3', '4' ], [ 0, 0, 0, 0 ] ); // $ExpectType string[]
30+
}
31+
32+
// The compiler throws an error if the function is provided a first argument which is not an array-like object...
33+
{
34+
mskreject( 1, [ 0, 0 ] ); // $ExpectError
35+
mskreject( true, [ 0, 0 ] ); // $ExpectError
36+
mskreject( false, [ 0, 0 ] ); // $ExpectError
37+
mskreject( null, [ 0, 0 ] ); // $ExpectError
38+
mskreject( void 0, [ 0, 0 ] ); // $ExpectError
39+
mskreject( {}, [ 0, 0 ] ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers...
43+
{
44+
mskreject( [], 1 ); // $ExpectError
45+
mskreject( [], true ); // $ExpectError
46+
mskreject( [], false ); // $ExpectError
47+
mskreject( [], null ); // $ExpectError
48+
mskreject( [], void 0 ); // $ExpectError
49+
mskreject( [], {} ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
mskreject(); // $ExpectError
55+
mskreject( [] ); // $ExpectError
56+
mskreject( [], [], [] ); // $ExpectError
57+
}

0 commit comments

Comments
 (0)