(dir)
| 176 | |
| 177 | // Create a reducing function iterating left or right. |
| 178 | function createReduce(dir) { |
| 179 | // Optimized iterator function as using arguments.length |
| 180 | // in the main function will deoptimize the, see #1991. |
| 181 | function iterator(obj, iteratee, memo, keys, index, length) { |
| 182 | for (; index >= 0 && index < length; index += dir) { |
| 183 | var currentKey = keys ? keys[index] : index; |
| 184 | memo = iteratee(memo, obj[currentKey], currentKey, obj); |
| 185 | } |
| 186 | return memo; |
| 187 | } |
| 188 | |
| 189 | return function(obj, iteratee, memo, context) { |
| 190 | iteratee = optimizeCb(iteratee, context, 4); |
| 191 | var keys = !isArrayLike(obj) && _.keys(obj), |
| 192 | length = (keys || obj).length, |
| 193 | index = dir > 0 ? 0 : length - 1; |
| 194 | // Determine the initial value if none is provided. |
| 195 | if (arguments.length < 3) { |
| 196 | memo = obj[keys ? keys[index] : index]; |
| 197 | index += dir; |
| 198 | } |
| 199 | return iterator(obj, iteratee, memo, keys, index, length); |
| 200 | }; |
| 201 | } |
| 202 | |
| 203 | // **Reduce** builds up a single result from a list of values, aka `inject`, |
| 204 | // or `foldl`. |
no test coverage detected