repl.txt (6420B)
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, iterating from right to left. 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 = [ 1000, 2500, 3000 ]; 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 = [ 1000, 2500, 3000 ]; 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 = [ 1000, 2500, 3000 ]; 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, iterating from 164 right to left. 165 166 Parameters 167 ---------- 168 options: Object (optional) 169 Function options. 170 171 options.limit: integer (optional) 172 Maximum number of pending invocations. Default: Infinity. 173 174 options.series: boolean (optional) 175 Boolean indicating whether to process each collection element 176 sequentially. Default: true. 177 178 options.thisArg: any (optional) 179 Execution context. 180 181 fcn: Function 182 The function to invoke for each element in a collection. 183 184 Returns 185 ------- 186 out: Function 187 A function which invokes a function for each element in a collection. 188 189 Examples 190 -------- 191 > function fcn( acc, value, index, next ) { 192 ... setTimeout( onTimeout, value ); 193 ... function onTimeout() { 194 ... console.log( value ); 195 ... acc.sum += value; 196 ... next( null, acc ); 197 ... } 198 ... }; 199 > var opts = { 'series': false }; 200 > var f = {{alias}}.factory( opts, fcn ); 201 > function done( error, acc ) { 202 ... if ( error ) { 203 ... throw error; 204 ... } 205 ... console.log( acc.sum ); 206 ... }; 207 > var arr = [ 1000, 2500, 3000 ]; 208 > var acc = { 'sum': 0 }; 209 > f( arr, acc, done ) 210 1000 211 2500 212 3000 213 6500 214 > acc = { 'sum': 0 }; 215 > arr = [ 1000, 1500, 2000 ]; 216 > f( arr, acc, done ) 217 1000 218 1500 219 2000 220 4500 221 222 See Also 223 -------- 224