diff options
Diffstat (limited to 'src/sjs.mjs')
| -rw-r--r-- | src/sjs.mjs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/sjs.mjs b/src/sjs.mjs index 2c9d551..e9da7fc 100644 --- a/src/sjs.mjs +++ b/src/sjs.mjs @@ -294,9 +294,8 @@ export const reduced = x => new Reduced(x); const reduceRec = (coll, fn, acc, index) => isReduced(acc) ? acc.value : - index === coll.length - ? acc - : reduceRec( + index === coll.length ? acc : + reduceRec( coll, fn, fn(acc, coll[index], index, coll), @@ -308,6 +307,30 @@ export const reduce = (coll, fn, init) => ? reduceRec(coll.slice(1), fn, coll[0], 0) : reduceRec(coll, fn, init, 0); +const reductionsRec = (coll, fn, acc, index, steps) => { + if (isReduced(acc)) { + return steps.concat(acc.value); + } + + if (index === coll.length) { + return steps; + } + + const newAcc = fn(acc, coll[index], index, coll); + return reductionsRec( + coll, + fn, + newAcc, + index + 1, + steps.concat(newAcc), + ); +}; + +export const reductions = (coll, fn, init) => + init === undefined + ? reductionsRec(coll.slice(1), fn, coll[0], 0, [coll[0]]) + : reductionsRec(coll, fn, init, 0, []); + export const mapValues = (obj, fn) => Object.keys(obj).reduce( (o, k) => ({ |
