Skip to content

Commit e91a47c

Browse files
committed
feat: add array/base/bifurcate-entries
1 parent 2965e0b commit e91a47c

File tree

10 files changed

+789
-0
lines changed

10 files changed

+789
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# bifurcateEntries
22+
23+
> Split array element entries into two groups.
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 bifurcateEntries = require( '@stdlib/array/base/bifurcate-entries' );
41+
```
42+
43+
#### bifurcateEntries( x, filter )
44+
45+
Splits array element entries into two groups.
46+
47+
```javascript
48+
var x = [ 'beep', 'boop', 'foo', 'bar' ];
49+
var filter = [ true, true, false, true ];
50+
51+
var out = bifurcateEntries( x, filter );
52+
// returns [ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
53+
```
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
60+
61+
<section class="notes">
62+
63+
## Notes
64+
65+
- If an element in `filter` is truthy, the corresponding element in the input array `x` belongs to the first group; otherwise, the input array element belongs to the second group.
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<!-- Package usage examples. -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
81+
var take = require( '@stdlib/array/base/take' );
82+
var bifurcateEntries = require( '@stdlib/array/base/bifurcate-entries' );
83+
84+
// Define an initial array of values:
85+
var values = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ];
86+
87+
// Sample from the initial array to generate a random collection:
88+
var indices = discreteUniform( 100, 0, values.length-1, {
89+
'dtype': 'generic'
90+
});
91+
var x = take( values, indices );
92+
// returns [...]
93+
94+
// Randomly assign collection values to groups:
95+
var groups = discreteUniform( x.length, 0, 1, {
96+
'dtype': 'generic'
97+
});
98+
99+
// Group the values:
100+
var out = bifurcateEntries( x, groups );
101+
// returns [...]
102+
103+
console.log( out );
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- 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. -->
111+
112+
<section class="references">
113+
114+
</section>
115+
116+
<!-- /.references -->
117+
118+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
119+
120+
<section class="related">
121+
122+
</section>
123+
124+
<!-- /.related -->
125+
126+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
127+
128+
<section class="links">
129+
130+
</section>
131+
132+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isArrayArray = require( '@stdlib/assert/is-array-array' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
27+
var zeroTo = require( '@stdlib/array/base/zero-to' );
28+
var pkg = require( './../package.json' ).name;
29+
var bifurcateEntries = 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 filter;
43+
var x;
44+
45+
x = zeroTo( len );
46+
filter = discreteUniform( len, 0, 1, {
47+
'dtype': 'generic'
48+
});
49+
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var out;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
out = bifurcateEntries( x, filter );
65+
if ( typeof out !== 'object' ) {
66+
b.fail( 'should return an object' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isArrayArray( out ) ) {
71+
b.fail( 'should return an object' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
99+
f = createBenchmark( len );
100+
bench( pkg+':len='+len, f );
101+
}
102+
}
103+
104+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( x, filter )
3+
Splits array element entries into two groups.
4+
5+
If an element in `filter` is truthy, then the corresponding element in the
6+
input array belongs to the first group; otherwise, the array element belongs
7+
to the second group.
8+
9+
If provided an empty array, the function returns an empty array.
10+
11+
Parameters
12+
----------
13+
x: ArrayLike
14+
Input array.
15+
16+
filter: ArrayLike
17+
An array indicating which group an element in the input array
18+
belongs to. If an element in `filter` is truthy, the corresponding
19+
element in the input array belongs to the first group; otherwise, the
20+
array element belongs to the second group.
21+
22+
Returns
23+
-------
24+
out: Array<Array>
25+
Group results.
26+
27+
Examples
28+
--------
29+
> var x = [ 'beep', 'boop', 'foo', 'bar' ];
30+
> var f = [ true, true, false, true ];
31+
> var out = {{alias}}( x, f )
32+
[ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
33+
> f = [ 1, 1, 0, 1 ];
34+
> out = {{alias}}( x, f )
35+
[ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
36+
37+
See Also
38+
--------
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Group results.
27+
*/
28+
type Results<T> = [ Array<[ number, T ]>, Array<[ number, T ]> ];
29+
30+
/**
31+
* Splits array element entries into two groups.
32+
*
33+
* @param x - input array
34+
* @param filter - array indicating which group an element in the input array belongs to
35+
* @returns results
36+
*
37+
* @example
38+
* var x = [ 'beep', 'boop', 'foo', 'bar' ];
39+
* var filter = [ true, true, false, true ];
40+
*
41+
* var out = bifurcateEntries( arr, filter );
42+
* // returns [ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
43+
*/
44+
declare function bifurcateEntries<T = unknown>( x: Collection<T> | AccessorArrayLike<T>, filter: Collection | AccessorArrayLike<any> ): Results<T>; // eslint-disable-line @typescript-eslint/no-explicit-any
45+
46+
47+
// EXPORTS //
48+
49+
export = bifurcateEntries;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 bifurcateEntries = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns group results...
25+
{
26+
const x = [ 1, 2, 3 ];
27+
const f = [ 0, 1, 0 ];
28+
29+
bifurcateEntries( x, f ); // $ExpectType Results<number>
30+
}
31+
32+
// The compiler throws an error if the function is provided a first argument which is not an array...
33+
{
34+
const f = [ 0, 1, 0 ];
35+
36+
bifurcateEntries( 5, f ); // $ExpectError
37+
bifurcateEntries( true, f ); // $ExpectError
38+
bifurcateEntries( false, f ); // $ExpectError
39+
bifurcateEntries( null, f ); // $ExpectError
40+
bifurcateEntries( void 0, f ); // $ExpectError
41+
bifurcateEntries( {}, f ); // $ExpectError
42+
bifurcateEntries( ( x: number ): number => x, f ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided a second argument which is not an array...
46+
{
47+
const x = [ 1, 2, 3 ];
48+
49+
bifurcateEntries( x, 5 ); // $ExpectError
50+
bifurcateEntries( x, true ); // $ExpectError
51+
bifurcateEntries( x, false ); // $ExpectError
52+
bifurcateEntries( x, null ); // $ExpectError
53+
bifurcateEntries( x, void 0 ); // $ExpectError
54+
bifurcateEntries( x, {} ); // $ExpectError
55+
bifurcateEntries( x, ( x: number ): number => x ); // $ExpectError
56+
}
57+
58+
// The compiler throws an error if the function is provided an unsupported number of arguments...
59+
{
60+
const x = [ 1, 2, 3 ];
61+
const f = [ 0, 1, 0 ];
62+
63+
bifurcateEntries(); // $ExpectError
64+
bifurcateEntries( x ); // $ExpectError
65+
bifurcateEntries( x, f, {} ); // $ExpectError
66+
}

0 commit comments

Comments
 (0)