From 46bdf5d79de07a4776c47725ea8fc2cf60db58f3 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 14 Dec 2015 08:25:28 -0800 Subject: [PATCH] Simplify `_.reduce` and `_.reduceRight`. --- lodash.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lodash.js b/lodash.js index 87257f101..450ac9e1d 100644 --- a/lodash.js +++ b/lodash.js @@ -7701,11 +7701,10 @@ * // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed) */ function reduce(collection, iteratee, accumulator) { - var initFromCollection = arguments.length < 3; - iteratee = getIteratee(iteratee, 4); - return isArray(collection) - ? arrayReduce(collection, iteratee, accumulator, initFromCollection) - : baseReduce(collection, iteratee, accumulator, initFromCollection, baseEach); + var func = isArray(collection) ? arrayReduce : baseReduce, + initFromCollection = arguments.length < 3; + + return func(collection, getIteratee(iteratee, 4), accumulator, initFromCollection, baseEach); } /** @@ -7729,11 +7728,10 @@ * // => [4, 5, 2, 3, 0, 1] */ function reduceRight(collection, iteratee, accumulator) { - var initFromCollection = arguments.length < 3; - iteratee = getIteratee(iteratee, 4); - return isArray(collection) - ? arrayReduceRight(collection, iteratee, accumulator, initFromCollection) - : baseReduce(collection, iteratee, accumulator, initFromCollection, baseEachRight); + var func = isArray(collection) ? arrayReduceRight : baseReduce, + initFromCollection = arguments.length < 3; + + return func(collection, getIteratee(iteratee, 4), accumulator, initFromCollection, baseEachRight); } /**