README.md (2741B)
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 # mapFun 22 23 > Invoke a function `n` times and return an array of accumulated function return values. 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 mapFun = require( '@stdlib/utils/map-function' ); 41 ``` 42 43 #### mapFun( fcn, n\[, thisArg ] ) 44 45 Invokes a function `n` times and returns an `array` of accumulated function return values. 46 47 ```javascript 48 function fcn( i ) { 49 return i; 50 } 51 52 var arr = mapFun( fcn, 5 ); 53 // returns [ 0, 1, 2, 3, 4 ] 54 ``` 55 56 To set the function execution context, provide a `thisArg`. 57 58 ```javascript 59 function fcn( i ) { 60 this.count += 1; 61 return i; 62 } 63 64 var context = { 65 'count': 0 66 }; 67 68 var arr = mapFun( fcn, 5, context ); 69 // returns [ 0, 1, 2, 3, 4 ] 70 71 console.log( context.count ); 72 // => 5 73 ``` 74 75 </section> 76 77 <!-- /.usage --> 78 79 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 80 81 <section class="notes"> 82 83 ## Notes 84 85 - The invoked `function` is provided a single argument: the invocation index (zero-based). 86 87 </section> 88 89 <!-- /.notes --> 90 91 <!-- Package usage examples. --> 92 93 <section class="examples"> 94 95 ## Examples 96 97 <!-- eslint no-undef: "error" --> 98 99 ```javascript 100 var randu = require( '@stdlib/random/base/randu' ); 101 var mapFun = require( '@stdlib/utils/map-function' ); 102 103 function rand( i ) { 104 return randu() * i * 10.0; 105 } 106 107 var arr = mapFun( rand, 100 ); 108 console.log( arr ); 109 ``` 110 111 </section> 112 113 <!-- /.examples --> 114 115 <!-- 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. --> 116 117 <section class="references"> 118 119 </section> 120 121 <!-- /.references --> 122 123 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 124 125 <section class="links"> 126 127 </section> 128 129 <!-- /.links -->