|
| 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 | +# takeIndexed |
| 22 | + |
| 23 | +> Take elements from an indexed array. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### takeIndexed( x, indices ) |
| 34 | + |
| 35 | +Takes elements from an indexed array. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = [ 1, 2, 3, 4 ]; |
| 39 | + |
| 40 | +var y = takeIndexed( x, [ 1, 3 ] ); |
| 41 | +// returns [ 2, 4 ] |
| 42 | +``` |
| 43 | + |
| 44 | +If `indices` is an empty array, the function returns an empty array. |
| 45 | + |
| 46 | +```javascript |
| 47 | +var x = [ 1, 2, 3, 4 ]; |
| 48 | + |
| 49 | +var y = takeIndexed( x, [] ); |
| 50 | +// returns [] |
| 51 | +``` |
| 52 | + |
| 53 | +</section> |
| 54 | + |
| 55 | +<!-- /.usage --> |
| 56 | + |
| 57 | +<section class="notes"> |
| 58 | + |
| 59 | +## Notes |
| 60 | + |
| 61 | +- The function does **not** perform bounds checking. If an index is less than zero or greater than the maximum index of `x`, the value of the corresponding element in the output array is undefined. |
| 62 | +- An _indexed_ array-like object is a data structure in which one retrieves elements via integer indices using bracket `[]` notation (e.g., `Float64Array`, `Int32Array`, `Array`, etc). This is in contrast to an _accessor_ array-like object in which one retrieves elements using `get` and `set` methods (e.g., `Complex64Array` and `Complex128Array`). |
| 63 | + |
| 64 | +</section> |
| 65 | + |
| 66 | +<!-- /.notes --> |
| 67 | + |
| 68 | +<section class="examples"> |
| 69 | + |
| 70 | +## Examples |
| 71 | + |
| 72 | +<!-- eslint no-undef: "error" --> |
| 73 | + |
| 74 | +```javascript |
| 75 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 76 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 77 | +var linspace = require( '@stdlib/array/base/linspace' ); |
| 78 | +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); |
| 79 | + |
| 80 | +// Generate a linearly spaced array: |
| 81 | +var x = linspace( 0, 100, 11 ); |
| 82 | + |
| 83 | +// Generate an array of random indices: |
| 84 | +var N = discreteUniform( 5, 15 ); |
| 85 | +var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) ); |
| 86 | + |
| 87 | +// Take a random sample of elements from `x`: |
| 88 | +var y = takeIndexed( x, indices ); |
| 89 | + |
| 90 | +console.log( x ); |
| 91 | +console.log( indices ); |
| 92 | +console.log( y ); |
| 93 | +``` |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.examples --> |
| 98 | + |
| 99 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 100 | + |
| 101 | +<section class="related"> |
| 102 | + |
| 103 | +</section> |
| 104 | + |
| 105 | +<!-- /.related --> |
| 106 | + |
| 107 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 108 | + |
| 109 | +<section class="links"> |
| 110 | + |
| 111 | +</section> |
| 112 | + |
| 113 | +<!-- /.links --> |
0 commit comments