|
| 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 | +# bifurcateIndicesBy |
| 22 | + |
| 23 | +> Split element indices into two groups according to an 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 bifurcateIndicesBy = require( '@stdlib/array/base/bifurcate-indices-by' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### bifurcateIndicesBy( x, predicate\[, thisArg] ) |
| 44 | + |
| 45 | +Splits element indices into two groups according to an predicate function. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function predicate( v ) { |
| 49 | + return v[ 0 ] === 'b'; |
| 50 | +} |
| 51 | + |
| 52 | +var x = [ 'beep', 'boop', 'foo', 'bar' ]; |
| 53 | + |
| 54 | +var out = bifurcateIndicesBy( x, predicate ); |
| 55 | +// returns [ [ 0, 1, 3 ], [ 2 ] ] |
| 56 | +``` |
| 57 | + |
| 58 | +An `predicate` function is provided the following 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( v ) { |
| 68 | + this.count += 1; |
| 69 | + return v[ 0 ] === 'b'; |
| 70 | +} |
| 71 | + |
| 72 | +var x = [ 'beep', 'boop', 'foo', 'bar' ]; |
| 73 | + |
| 74 | +var context = { |
| 75 | + 'count': 0 |
| 76 | +}; |
| 77 | +var out = bifurcateIndicesBy( x, predicate, context ); |
| 78 | +// returns [ [ 0, 1, 3 ], [ 2 ] ] |
| 79 | + |
| 80 | +var cnt = context.count; |
| 81 | +// returns 4 |
| 82 | +``` |
| 83 | + |
| 84 | +</section> |
| 85 | + |
| 86 | +<!-- /.usage --> |
| 87 | + |
| 88 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 89 | + |
| 90 | +<section class="notes"> |
| 91 | + |
| 92 | +</section> |
| 93 | + |
| 94 | +<!-- /.notes --> |
| 95 | + |
| 96 | +<!-- Package usage examples. --> |
| 97 | + |
| 98 | +<section class="examples"> |
| 99 | + |
| 100 | +## Examples |
| 101 | + |
| 102 | +<!-- eslint no-undef: "error" --> |
| 103 | + |
| 104 | +```javascript |
| 105 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 106 | +var take = require( '@stdlib/array/base/take' ); |
| 107 | +var bifurcateIndicesBy = require( '@stdlib/array/base/bifurcate-indices-by' ); |
| 108 | + |
| 109 | +function predicate( v ) { |
| 110 | + // Use the first letter of each element to define groups: |
| 111 | + return v[ 0 ] === 'b'; |
| 112 | +} |
| 113 | + |
| 114 | +// Define an initial array of values: |
| 115 | +var values = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ]; |
| 116 | + |
| 117 | +// Sample from the initial array to generate a random collection: |
| 118 | +var indices = discreteUniform( 100, 0, values.length-1, { |
| 119 | + 'dtype': 'generic' |
| 120 | +}); |
| 121 | +var x = take( values, indices ); |
| 122 | +// returns [...] |
| 123 | + |
| 124 | +// Split the values: |
| 125 | +var out = bifurcateIndicesBy( x, predicate ); |
| 126 | +// returns [...] |
| 127 | + |
| 128 | +console.log( out ); |
| 129 | +``` |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.examples --> |
| 134 | + |
| 135 | +<!-- 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. --> |
| 136 | + |
| 137 | +<section class="references"> |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.references --> |
| 142 | + |
| 143 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 144 | + |
| 145 | +<section class="related"> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.related --> |
| 150 | + |
| 151 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 152 | + |
| 153 | +<section class="links"> |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.links --> |
0 commit comments