README.md (5302B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # inmap 22 23 > Invoke a function for each element in a collection and update the collection in-place. 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 inmap = require( '@stdlib/utils/inmap' ); 41 ``` 42 43 #### inmap( collection, fcn\[, thisArg ] ) 44 45 Invokes a `function` for each element in a `collection` and updates the `collection` in-place. 46 47 ```javascript 48 function scale( value, index ) { 49 return value * index; 50 } 51 52 var arr = [ 1, 2, 3, 4 ]; 53 54 var out = inmap( arr, scale ); 55 // returns [ 0, 2, 6, 12 ] 56 57 var bool = ( out === arr ); 58 // returns true 59 ``` 60 61 The invoked `function` is provided three arguments: 62 63 - `value`: collection element 64 - `index`: collection index 65 - `collection`: input collection 66 67 Basic support for dynamic collections is provided. Note, however, that index incrementation is monotonically increasing. 68 69 ```javascript 70 function scale1( value, index, collection ) { 71 if ( index === collection.length-1 && collection.length < 10 ) { 72 collection.push( index+2 ); 73 } 74 return value * index; 75 } 76 77 var arr = [ 1, 2, 3, 4 ]; 78 79 var out = inmap( arr, scale1 ); 80 // returns [ 0, 2, 6, 12, 20, 30, 42, 56, 72, 90 ] 81 82 function scale2( value, index, collection ) { 83 collection.shift(); 84 return value * index; 85 } 86 87 arr = [ 1, 2, 3, 4 ]; 88 89 out = inmap( arr, scale2 ); 90 // returns [ 3, 3 ] 91 ``` 92 93 To set the function execution context, provide a `thisArg`. 94 95 ```javascript 96 function sum( value ) { 97 this.sum += value; 98 this.count += 1; 99 return value; 100 } 101 102 var arr = [ 1, 2, 3, 4 ]; 103 104 var context = { 105 'sum': 0, 106 'count': 0 107 }; 108 109 var out = inmap( arr, sum, context ); 110 // returns [ 1, 2, 3, 4 ] 111 112 var mean = context.sum / context.count; 113 // returns 2.5 114 ``` 115 116 </section> 117 118 <!-- /.usage --> 119 120 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 121 122 <section class="notes"> 123 124 ## Notes 125 126 - A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). 127 128 - The function differs from [`Array.prototype.map`][mdn-array-map] in the following ways: 129 130 - The function returns the input `collection`. 131 132 - The function modifies `collection` elements in-place. 133 134 - The function does **not** skip `undefined` elements. 135 136 <!-- eslint-disable no-sparse-arrays, stdlib/doctest-marker --> 137 138 ```javascript 139 function log( value, index ) { 140 console.log( '%s: %s', index, value ); 141 return value; 142 } 143 144 var arr = [ 1, , , 4 ]; 145 146 var out = inmap( arr, log ); 147 /* => 148 0: 1 149 1: undefined 150 2: undefined 151 3: 4 152 */ 153 ``` 154 155 - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution). 156 157 </section> 158 159 <!-- /.notes --> 160 161 <!-- Package usage examples. --> 162 163 <section class="examples"> 164 165 ## Examples 166 167 <!-- eslint no-undef: "error" --> 168 169 ```javascript 170 var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; 171 var inmap = require( '@stdlib/utils/inmap' ); 172 173 var bool; 174 var arr; 175 var out; 176 var i; 177 178 function scale( value, index, collection ) { 179 if ( isEven( index ) ) { 180 collection.shift(); 181 } else { 182 collection.push( index+1 ); 183 } 184 return value * index; 185 } 186 187 arr = new Array( 100 ); 188 for ( i = 0; i < arr.length; i++ ) { 189 arr[ i ] = i; 190 } 191 out = inmap( arr, scale ); 192 193 bool = ( out === arr ); 194 console.log( bool ); 195 196 console.log( out ); 197 ``` 198 199 </section> 200 201 <!-- /.examples --> 202 203 <!-- 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. --> 204 205 <section class="references"> 206 207 </section> 208 209 <!-- /.references --> 210 211 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 212 213 <section class="links"> 214 215 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 216 217 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 218 219 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 220 221 [mdn-array-map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map 222 223 </section> 224 225 <!-- /.links -->