|
| 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 | +# takeMap |
| 22 | + |
| 23 | +> Take elements from an array and return a new array after applying a mapping function. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var takeMap = require( '@stdlib/array/base/take-map' ); |
| 31 | +``` |
| 32 | + |
| 33 | +### takeMap( x, indices, mode, clbk ) |
| 34 | + |
| 35 | +Takes elements from an array and returns a new array after applying a mapping function. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = [ 1, 2, 3, 4 ]; |
| 39 | + |
| 40 | +function customMapping( value ) { |
| 41 | + return value * 2; |
| 42 | +} |
| 43 | + |
| 44 | +var y = takeMap( x, [ 1, 3 ], 'throw', customMapping ); |
| 45 | +// returns [ 4, 8 ] |
| 46 | +``` |
| 47 | + |
| 48 | +The function supports the following parameters: |
| 49 | + |
| 50 | +- **x**: input array. |
| 51 | +- **indices**: list of indices. |
| 52 | +- **mode**: index [mode][@stdlib/ndarray/base/ind]. |
| 53 | +- **clbk**: function to apply. |
| 54 | + |
| 55 | +### takeMap.assign( x, indices, mode, out, stride, offset, clbk ) |
| 56 | + |
| 57 | +> Takes elements from an array and assigns the values to elements in a provided output array. |
| 58 | +
|
| 59 | +```javascript |
| 60 | +var x = [ 1, 2, 3, 4 ]; |
| 61 | + |
| 62 | +var out = [ 0, 0, 0, 0, 0, 0 ]; |
| 63 | +var indices = [ 0, 0, 1, 1, 3, 3 ]; |
| 64 | + |
| 65 | +function clbk( val ) { |
| 66 | + return val * 2; |
| 67 | +} |
| 68 | + |
| 69 | +var arr = takeMap.assign( x, indices, 'throw', out, -1, out.length-1, clbk ); |
| 70 | +// returns [ 8, 8, 4, 4, 2, 2 ] |
| 71 | + |
| 72 | +var bool = ( arr === out ); |
| 73 | +// returns true |
| 74 | +``` |
| 75 | + |
| 76 | +The function supports the following parameters: |
| 77 | + |
| 78 | +- **x**: input array. |
| 79 | +- **indices**: list of indices. |
| 80 | +- **mode**: index [mode][@stdlib/ndarray/base/ind]. |
| 81 | +- **out**: output array. |
| 82 | +- **stride**: output array stride. |
| 83 | +- **offset**: output array offset. |
| 84 | +- **clbk**: callback function. |
| 85 | + |
| 86 | +</section> |
| 87 | + |
| 88 | +<!-- /.usage --> |
| 89 | + |
| 90 | +<section class="notes"> |
| 91 | + |
| 92 | +</section> |
| 93 | + |
| 94 | +<!-- /.notes --> |
| 95 | + |
| 96 | +<section class="examples"> |
| 97 | + |
| 98 | +## Examples |
| 99 | + |
| 100 | +<!-- eslint no-undef: "error" --> |
| 101 | + |
| 102 | +```javascript |
| 103 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 104 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 105 | +var linspace = require( '@stdlib/array/base/linspace' ); |
| 106 | +var takeMap = require( '@stdlib/array/base/take-map' ); |
| 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 N = discreteUniform( 5, 15 ); |
| 114 | +var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) ); |
| 115 | +console.log( indices ); |
| 116 | + |
| 117 | +// Define a mapping function (e.g., square the value): |
| 118 | +function square( val ) { |
| 119 | + return val * val; |
| 120 | +} |
| 121 | + |
| 122 | +// Take a random sample of elements from `x` and apply the mapping function: |
| 123 | +var y = takeMap( x, indices, 'throw', square ); |
| 124 | +console.log( y ); |
| 125 | +``` |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.examples --> |
| 130 | + |
| 131 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 132 | + |
| 133 | +<section class="related"> |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.related --> |
| 138 | + |
| 139 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 140 | + |
| 141 | +<section class="links"> |
| 142 | + |
| 143 | +[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.links --> |
0 commit comments