|
| 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 | +# take |
| 22 | + |
| 23 | +> Take elements from an array. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var take = require( '@stdlib/array/take' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### take( x, indices\[, options] ) |
| 34 | + |
| 35 | +Takes elements from an array. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = [ 1, 2, 3, 4 ]; |
| 39 | + |
| 40 | +var y = take( x, [ 1, 3 ] ); |
| 41 | +// returns [ 2, 4 ] |
| 42 | +``` |
| 43 | + |
| 44 | +The function supports the following parameters: |
| 45 | + |
| 46 | +- **x**: input array. |
| 47 | +- **indices**: list of indices. |
| 48 | +- **options**: function options. |
| 49 | + |
| 50 | +The function supports the following options: |
| 51 | + |
| 52 | +- **mode**: index [mode][@stdlib/ndarray/base/ind]. Default: `'normalize'`. |
| 53 | + |
| 54 | +By default, the function normalizes negative integer indices to positive integer index equivalents. |
| 55 | + |
| 56 | +```javascript |
| 57 | +var x = [ 1, 2, 3, 4 ]; |
| 58 | + |
| 59 | +var y = take( x, [ -3, -1 ] ); |
| 60 | +// returns [ 2, 4 ] |
| 61 | +``` |
| 62 | + |
| 63 | +To specify an alternative index [mode][@stdlib/ndarray/base/ind], provide a `mode` option. |
| 64 | + |
| 65 | +```javascript |
| 66 | +var x = [ 1, 2, 3, 4 ]; |
| 67 | + |
| 68 | +var y = take( x, [ -10, 10 ], { |
| 69 | + 'mode': 'clamp' |
| 70 | +}); |
| 71 | +// returns [ 1, 4 ] |
| 72 | +``` |
| 73 | + |
| 74 | +</section> |
| 75 | + |
| 76 | +<!-- /.usage --> |
| 77 | + |
| 78 | +<section class="notes"> |
| 79 | + |
| 80 | +## Notes |
| 81 | + |
| 82 | +- If `indices` is an empty array, the function returns an empty array. |
| 83 | + |
| 84 | + ```javascript |
| 85 | + var x = [ 1, 2, 3, 4 ]; |
| 86 | + |
| 87 | + var y = take( x, [] ); |
| 88 | + // returns [] |
| 89 | + ``` |
| 90 | + |
| 91 | +- If provided an input array having a recognized [data type][@stdlib/array/dtypes], the function returns an array having the same [data type][@stdlib/array/dtypes] as the input array. Otherwise, the function **always** returns a "generic" array. |
| 92 | + |
| 93 | +</section> |
| 94 | + |
| 95 | +<!-- /.notes --> |
| 96 | + |
| 97 | +<section class="examples"> |
| 98 | + |
| 99 | +## Examples |
| 100 | + |
| 101 | +<!-- eslint no-undef: "error" --> |
| 102 | + |
| 103 | +```javascript |
| 104 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 105 | +var linspace = require( '@stdlib/array/linspace' ); |
| 106 | +var take = require( '@stdlib/array/take' ); |
| 107 | + |
| 108 | +// Generate a linearly spaced array: |
| 109 | +var x = linspace( 0, 100, 11 ); |
| 110 | +console.log( x ); |
| 111 | + |
| 112 | +// Generate an array of random indices: |
| 113 | +var indices = discreteUniform( 10, 0, x.length-1 ); |
| 114 | +console.log( indices ); |
| 115 | + |
| 116 | +// Take a random sample of elements from `x`: |
| 117 | +var y = take( x, indices ); |
| 118 | +console.log( y ); |
| 119 | +``` |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.examples --> |
| 124 | + |
| 125 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 126 | + |
| 127 | +<section class="related"> |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.related --> |
| 132 | + |
| 133 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 134 | + |
| 135 | +<section class="links"> |
| 136 | + |
| 137 | +[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind |
| 138 | + |
| 139 | +[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes |
| 140 | + |
| 141 | +</section> |
| 142 | + |
| 143 | +<!-- /.links --> |
0 commit comments