|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# map |
| 22 | + |
| 23 | +> Apply a callback function to elements in an input array and assign results to elements in a new output array. |
| 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 map = require( '@stdlib/array/base/map' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### map( x, fcn\[, thisArg] ) |
| 44 | + |
| 45 | +Applies a callback function to elements in an input array and assigns results to elements in a new output array. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 49 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 50 | + |
| 51 | +var x = [ -1.0, -2.0, -3.0, -4.0 ]; |
| 52 | + |
| 53 | +var y = map( x, naryFunction( abs, 1 ) ); |
| 54 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 55 | +``` |
| 56 | + |
| 57 | +The function accepts the following arguments: |
| 58 | + |
| 59 | +- **x**: input array. |
| 60 | +- **fcn**: callback function. |
| 61 | +- **thisArg**: callback execution context (_optional_). |
| 62 | + |
| 63 | +To set the callback function's execution context, provide a `thisArg`. |
| 64 | + |
| 65 | +<!-- eslint-disable no-invalid-this --> |
| 66 | + |
| 67 | +```javascript |
| 68 | +function count( x ) { |
| 69 | + this.count += 1; |
| 70 | + return x; |
| 71 | +} |
| 72 | + |
| 73 | +var x = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 74 | + |
| 75 | +var ctx = { |
| 76 | + 'count': 0 |
| 77 | +}; |
| 78 | + |
| 79 | +var y = map( x, count, ctx ); |
| 80 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 81 | + |
| 82 | +var v = ctx.count; |
| 83 | +// returns 4 |
| 84 | +``` |
| 85 | + |
| 86 | +The callback function is provided the following arguments: |
| 87 | + |
| 88 | +- **value**: current array element. |
| 89 | +- **index**: current element index. |
| 90 | +- **arr**: input array. |
| 91 | + |
| 92 | +#### map.assign( x, y, stride, offset, fcn\[, thisArg] ) |
| 93 | + |
| 94 | +Applies a callback function to elements in an input array and assigns results to elements in an output array. |
| 95 | + |
| 96 | +```javascript |
| 97 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 98 | +var zeros = require( '@stdlib/array/base/zeros' ); |
| 99 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 100 | + |
| 101 | +var x = [ -1.0, -2.0, -3.0, -4.0 ]; |
| 102 | + |
| 103 | +var y = zeros( x.length ); |
| 104 | + |
| 105 | +var out = map.assign( x, y, 1, 0, naryFunction( abs, 1 ) ); |
| 106 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 107 | + |
| 108 | +var bool = ( out === y ); |
| 109 | +// returns true |
| 110 | +``` |
| 111 | + |
| 112 | +The function accepts the following arguments: |
| 113 | + |
| 114 | +- **x**: input array. |
| 115 | +- **y**: output array. |
| 116 | +- **stride**: stride length for output array. |
| 117 | +- **offset**: starting index for output array. |
| 118 | +- **fcn**: callback function. |
| 119 | +- **thisArg**: callback execution context (_optional_). |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.usage --> |
| 124 | + |
| 125 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 126 | + |
| 127 | +<section class="notes"> |
| 128 | + |
| 129 | +## Notes |
| 130 | + |
| 131 | +- If provided an array-like object having a `map` method, the function defers execution to that method and assumes that the method API has the following signature: |
| 132 | + |
| 133 | + ```text |
| 134 | + x.map( fcn, thisArg ) |
| 135 | + ``` |
| 136 | +
|
| 137 | +- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 138 | +
|
| 139 | +</section> |
| 140 | +
|
| 141 | +<!-- /.notes --> |
| 142 | +
|
| 143 | +<section class="examples"> |
| 144 | +
|
| 145 | +## Examples |
| 146 | +
|
| 147 | +<!-- eslint no-undef: "error" --> |
| 148 | +
|
| 149 | +```javascript |
| 150 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 151 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 152 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 153 | +var map = require( '@stdlib/array/base/map' ); |
| 154 | +
|
| 155 | +var x = discreteUniform( 10, -10, 10, { |
| 156 | + 'dtype': 'float64' |
| 157 | +}); |
| 158 | +
|
| 159 | +var y = map( x, naryFunction( abs, 1 ) ); |
| 160 | +console.log( y ); |
| 161 | +``` |
| 162 | + |
| 163 | +</section> |
| 164 | + |
| 165 | +<!-- /.examples --> |
| 166 | + |
| 167 | +<!-- 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. --> |
| 168 | + |
| 169 | +<section class="references"> |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.references --> |
| 174 | + |
| 175 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 176 | + |
| 177 | +<section class="related"> |
| 178 | + |
| 179 | +</section> |
| 180 | + |
| 181 | +<!-- /.related --> |
| 182 | + |
| 183 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 184 | + |
| 185 | +<section class="links"> |
| 186 | + |
| 187 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/array/tree/main/base/accessor |
| 188 | + |
| 189 | +</section> |
| 190 | + |
| 191 | +<!-- /.links --> |
0 commit comments