repl.txt (6356B)
1 2 {{alias}}( collection, initial, [options,] reducer, done ) 3 Applies a function against an accumulator and each element in a collection 4 and returns the accumulated result. 5 6 When invoked, `reducer` is provided a maximum of five arguments: 7 8 - `accumulator`: accumulated value 9 - `value`: collection value 10 - `index`: collection index 11 - `collection`: the input collection 12 - `next`: a callback to be invoked after processing a collection `value` 13 14 The actual number of provided arguments depends on function length. If 15 `reducer` accepts three arguments, `reducer` is provided: 16 17 - `accumulator` 18 - `value` 19 - `next` 20 21 If `reducer` accepts four arguments, `reducer` is provided: 22 23 - `accumulator` 24 - `value` 25 - `index` 26 - `next` 27 28 For every other `reducer` signature, `reducer` is provided all five 29 arguments. 30 31 The `next` callback accepts two arguments: 32 33 - `error`: error argument 34 - `accumulator`: accumulated value 35 36 If a provided function calls the `next` callback with a truthy `error` 37 argument, the function suspends execution and immediately calls the `done` 38 callback for subsequent `error` handling. 39 40 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 41 wrap the `done` callback in a function which either executes at the end of 42 the current stack (e.g., `nextTick`) or during a subsequent turn of the 43 event loop (e.g., `setImmediate`, `setTimeout`). 44 45 The function does not support dynamic collection resizing. 46 47 If provided an empty collection, the function invokes the `done` callback 48 with the `initial` value as the second argument. 49 50 The function does not skip `undefined` elements. 51 52 When processing collection elements concurrently, *beware* of race 53 conditions when updating an accumulator. This is especially true when an 54 accumulator is a primitive (e.g., a number). In general, prefer object 55 accumulators. 56 57 Parameters 58 ---------- 59 collection: Array|TypedArray|Object 60 Input collection over which to iterate. If provided an object, the 61 object must be array-like (excluding strings and functions). 62 63 initial: any 64 Accumulator value used in the first invocation of the reduction 65 function. 66 67 options: Object (optional) 68 Function options. 69 70 options.limit: integer (optional) 71 Maximum number of pending invocations. Default: Infinity. 72 73 options.series: boolean (optional) 74 Boolean indicating whether to process each collection element 75 sequentially. Default: true. 76 77 options.thisArg: any (optional) 78 Execution context. 79 80 reducer: Function 81 The function to invoke for each element in a collection. 82 83 done: Function 84 A callback invoked either upon processing all collection elements or 85 upon encountering an error. 86 87 Examples 88 -------- 89 // Basic usage: 90 > function fcn( acc, value, index, next ) { 91 ... setTimeout( onTimeout, value ); 92 ... function onTimeout() { 93 ... console.log( value ); 94 ... acc.sum += value; 95 ... next( null, acc ); 96 ... } 97 ... }; 98 > function done( error, acc ) { 99 ... if ( error ) { 100 ... throw error; 101 ... } 102 ... console.log( acc.sum ); 103 ... }; 104 > var arr = [ 3000, 2500, 1000 ]; 105 > var acc = { 'sum': 0 }; 106 > {{alias}}( arr, acc, fcn, done ) 107 3000 108 2500 109 1000 110 6500 111 112 // Limit number of concurrent invocations: 113 > function fcn( acc, value, index, next ) { 114 ... setTimeout( onTimeout, value ); 115 ... function onTimeout() { 116 ... console.log( value ); 117 ... acc.sum += value; 118 ... next( null, acc ); 119 ... } 120 ... }; 121 > function done( error, acc ) { 122 ... if ( error ) { 123 ... throw error; 124 ... } 125 ... console.log( acc.sum ); 126 ... }; 127 > var opts = { 'limit': 2 }; 128 > var arr = [ 3000, 2500, 1000 ]; 129 > var acc = { 'sum': 0 }; 130 > {{alias}}( arr, acc, opts, fcn, done ) 131 2500 132 3000 133 1000 134 6500 135 136 // Process concurrently: 137 > function fcn( acc, value, index, next ) { 138 ... setTimeout( onTimeout, value ); 139 ... function onTimeout() { 140 ... console.log( value ); 141 ... acc.sum += value; 142 ... next( null, acc ); 143 ... } 144 ... }; 145 > function done( error, acc ) { 146 ... if ( error ) { 147 ... throw error; 148 ... } 149 ... console.log( acc.sum ); 150 ... }; 151 > var opts = { 'series': false }; 152 > var arr = [ 3000, 2500, 1000 ]; 153 > var acc = { 'sum': 0 }; 154 > {{alias}}( arr, acc, opts, fcn, done ) 155 1000 156 2500 157 3000 158 6500 159 160 161 {{alias}}.factory( [options,] fcn ) 162 Returns a function which applies a function against an accumulator and each 163 element in a collection and returns the accumulated result. 164 165 Parameters 166 ---------- 167 options: Object (optional) 168 Function options. 169 170 options.limit: integer (optional) 171 Maximum number of pending invocations. Default: Infinity. 172 173 options.series: boolean (optional) 174 Boolean indicating whether to process each collection element 175 sequentially. Default: true. 176 177 options.thisArg: any (optional) 178 Execution context. 179 180 fcn: Function 181 The function to invoke for each element in a collection. 182 183 Returns 184 ------- 185 out: Function 186 A function which invokes a function for each element in a collection. 187 188 Examples 189 -------- 190 > function fcn( acc, value, index, next ) { 191 ... setTimeout( onTimeout, value ); 192 ... function onTimeout() { 193 ... console.log( value ); 194 ... acc.sum += value; 195 ... next( null, acc ); 196 ... } 197 ... }; 198 > var opts = { 'series': false }; 199 > var f = {{alias}}.factory( opts, fcn ); 200 > function done( error, acc ) { 201 ... if ( error ) { 202 ... throw error; 203 ... } 204 ... console.log( acc.sum ); 205 ... }; 206 > var arr = [ 3000, 2500, 1000 ]; 207 > var acc = { 'sum': 0 }; 208 > f( arr, acc, done ) 209 1000 210 2500 211 3000 212 6500 213 > acc = { 'sum': 0 }; 214 > arr = [ 2000, 1500, 1000 ]; 215 > f( arr, acc, done ) 216 1000 217 1500 218 2000 219 4500 220 221 See Also 222 -------- 223