Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Dec 30, 2023
1 parent 935e78e commit 6772024
Show file tree
Hide file tree
Showing 13 changed files with 803 additions and 5 deletions.
132 changes: 132 additions & 0 deletions base/bifurcate-entries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!--
@license Apache-2.0
Copyright (c) 2023 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# bifurcateEntries

> Split array element entries into two groups.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var bifurcateEntries = require( '@stdlib/array/base/bifurcate-entries' );
```

#### bifurcateEntries( x, filter )

Splits array element entries into two groups.

```javascript
var x = [ 'beep', 'boop', 'foo', 'bar' ];
var filter = [ true, true, false, true ];

var out = bifurcateEntries( x, filter );
// returns [ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- 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.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var take = require( '@stdlib/array/base/take' );
var bifurcateEntries = require( '@stdlib/array/base/bifurcate-entries' );

// Define an initial array of values:
var values = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ];

// Sample from the initial array to generate a random collection:
var indices = discreteUniform( 100, 0, values.length-1, {
'dtype': 'generic'
});
var x = take( values, indices );
// returns [...]

// Randomly assign collection values to groups:
var groups = discreteUniform( x.length, 0, 1, {
'dtype': 'generic'
});

// Group the values:
var out = bifurcateEntries( x, groups );
// returns [...]

console.log( out );
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
104 changes: 104 additions & 0 deletions base/bifurcate-entries/benchmark/benchmark.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isArrayArray = require( '@stdlib/assert/is-array-array' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var zeroTo = require( './../../../base/zero-to' );
var pkg = require( './../package.json' ).name;
var bifurcateEntries = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var filter;
var x;

x = zeroTo( len );
filter = discreteUniform( len, 0, 1, {
'dtype': 'generic'
});

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = bifurcateEntries( x, filter );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isArrayArray( out ) ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

f = createBenchmark( len );
bench( pkg+':len='+len, f );
}
}

main();
39 changes: 39 additions & 0 deletions base/bifurcate-entries/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

{{alias}}( x, filter )
Splits array element entries into two groups.

If an element in `filter` is truthy, then the corresponding element in the
input array belongs to the first group; otherwise, the array element belongs
to the second group.

If provided an empty array, the function returns an empty array.

Parameters
----------
x: ArrayLike
Input array.

filter: ArrayLike
An array indicating which group an element in the input array
belongs to. If an element in `filter` is truthy, the corresponding
element in the input array belongs to the first group; otherwise, the
array element belongs to the second group.

Returns
-------
out: Array<Array>
Group results.

Examples
--------
> var x = [ 'beep', 'boop', 'foo', 'bar' ];
> var f = [ true, true, false, true ];
> var out = {{alias}}( x, f )
[ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
> f = [ 1, 1, 0, 1 ];
> out = {{alias}}( x, f )
[ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]

See Also
--------

49 changes: 49 additions & 0 deletions base/bifurcate-entries/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection, AccessorArrayLike } from '@stdlib/types/array';

/**
* Group results.
*/
type Results<T> = [ Array<[ number, T ]>, Array<[ number, T ]> ];

/**
* Splits array element entries into two groups.
*
* @param x - input array
* @param filter - array indicating which group an element in the input array belongs to
* @returns results
*
* @example
* var x = [ 'beep', 'boop', 'foo', 'bar' ];
* var filter = [ true, true, false, true ];
*
* var out = bifurcateEntries( arr, filter );
* // returns [ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
*/
declare function bifurcateEntries<T = unknown>( x: Collection<T> | AccessorArrayLike<T>, filter: Collection | AccessorArrayLike<any> ): Results<T>; // eslint-disable-line @typescript-eslint/no-explicit-any


// EXPORTS //

export = bifurcateEntries;
66 changes: 66 additions & 0 deletions base/bifurcate-entries/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import bifurcateEntries = require( './index' );


// TESTS //

// The function returns group results...
{
const x = [ 1, 2, 3 ];
const f = [ 0, 1, 0 ];

bifurcateEntries( x, f ); // $ExpectType Results<number>
}

// The compiler throws an error if the function is provided a first argument which is not an array...
{
const f = [ 0, 1, 0 ];

bifurcateEntries( 5, f ); // $ExpectError
bifurcateEntries( true, f ); // $ExpectError
bifurcateEntries( false, f ); // $ExpectError
bifurcateEntries( null, f ); // $ExpectError
bifurcateEntries( void 0, f ); // $ExpectError
bifurcateEntries( {}, f ); // $ExpectError
bifurcateEntries( ( x: number ): number => x, f ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not an array...
{
const x = [ 1, 2, 3 ];

bifurcateEntries( x, 5 ); // $ExpectError
bifurcateEntries( x, true ); // $ExpectError
bifurcateEntries( x, false ); // $ExpectError
bifurcateEntries( x, null ); // $ExpectError
bifurcateEntries( x, void 0 ); // $ExpectError
bifurcateEntries( x, {} ); // $ExpectError
bifurcateEntries( x, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const x = [ 1, 2, 3 ];
const f = [ 0, 1, 0 ];

bifurcateEntries(); // $ExpectError
bifurcateEntries( x ); // $ExpectError
bifurcateEntries( x, f, {} ); // $ExpectError
}
Loading

0 comments on commit 6772024

Please sign in to comment.