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