Skip to content

Commit

Permalink
feat: Update "reduce" to allow uncurried call
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Aug 27, 2020
1 parent 4847d7b commit 69ce47d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
53 changes: 41 additions & 12 deletions src/reduce/reduce.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
import { pipe } from "../pipe/pipe"

const _reduce = (fn, defaultAcc, _source) => {
let acc = defaultAcc
const source = Array.isArray(_source) ? _source : [_source]

for (let i = 0, length = source.length; i < length; i++) {
acc = Array.isArray(fn)
? pipe(...fn)(acc, source[i], i, source)
: fn(acc, source[i], i, source)
}

return acc
}

/**
* Apply a function against an accumulator and each element in the array (from
* left to right) to reduce it to a single value.
*
* @param {Function} fn Reduce function
* @param {Object} defaultAcc The default acc
* @param {Array} source Source input
* @param {Function} fn Reduce function
* @param {Object} defaultAcc Default accumulator value
* @param {Array} source Source input
*
* @return {mixed}
*
* @tag Array
* @signature (fn: Function, defaultAcc: mixed) => (source: Array): mixed
* @signature (fn: Function, defaultAcc: mixed, source: Array): mixed
*
* @example
* const sum = (acc, item) => acc + item
*
* reduce(sum, 0, [1, 2])
* // => 3
*/
const reduce = (fn, defaultAcc) => source => {
let acc = defaultAcc
const sourceArray = Array.isArray(source) ? source : [source]

for (let i = 0, length = sourceArray.length; i < length; i++) {
acc = fn(acc, sourceArray[i], i, sourceArray)
export const reduce = (...params) => {
/*
* @signature (fn: Fn|Fn[], defaultAcc: mixed) => (source: []): mixed
*
* reduce(sum, 0)([1, 2])
* // => 3
*/
if (params.length < 3) {
return source => _reduce(params[0], params[1], source)
}

return acc
/*
* @signature (fn: Fn|Fn[], defaultAcc: mixed, source: []): mixed
*
* reduce(sum, 0, [1, 2])
* // => 3
*/
return _reduce(...params)
}

export { reduce }
14 changes: 12 additions & 2 deletions src/reduce/reduce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ import test from "tape"
import { reduce } from ".."

test("reduce", t => {
t.equals(reduce((acc, next) => acc + next, 0)([1, 2, 3]), 6, "Sum an array")
t.equals(
reduce((acc, item) => acc + item, 0, [1, 2, 3]),
6,
"Sum an array - uncurried"
)

t.equals(
reduce((acc, next) => acc + next, 0)([1, 2, 3]),
6,
"Sum an array-curried"
)

t.equals(
reduce((acc, next) => acc + next, 0)(12),
Expand All @@ -17,7 +27,7 @@ test("reduce", t => {
)

t.equals(
reduce((acc = 0, next) => acc + next, 0)([]),
reduce((acc, next) => acc + next, 0)([]),
0,
"Reducing empty array return default acc"
)
Expand Down

0 comments on commit 69ce47d

Please sign in to comment.