-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { pipe } from "../pipe/pipe" | ||
|
||
const _converge = (_accFn, _extractFn, source) => { | ||
const accFn = Array.isArray(_accFn) ? pipe(..._accFn) : _accFn | ||
const extractFn = Array.isArray(_extractFn) ? _extractFn : [_extractFn] | ||
const extractValues = [] | ||
|
||
for (let i = 0; i < extractFn.length; ++i) { | ||
extractValues.push(extractFn[i](source)) | ||
} | ||
|
||
return accFn(...extractValues) | ||
} | ||
|
||
/** | ||
* Apply a list of function, extract functions, on the same input and use | ||
* those results as parameters for another accumulator function. | ||
* | ||
* @param {Fn|Fn[]} accFn Accumulator or final aggreate function | ||
* @param {Fn|Fn[]} extractFn List of functions to be applied on input | ||
* @param {Any} source Source input | ||
* | ||
* @return {Any} | ||
* | ||
* @name converge | ||
* @tag Core | ||
* @signature (accFn: Fn|Fn[], extractFn: Fn|Fn[]) => (source: Array): Any | ||
* @signature (accFn: Fn|Fn[], extractFn: Fn|Fn[], source: Array): Any | ||
* | ||
* @example | ||
* const divide = () => ... | ||
* const sum = () => ... | ||
* const count = () => ... | ||
* | ||
* converge(divide, [sum, count], [1, 2, 3, 4, 5, 6, 7]) | ||
* // => 4 | ||
*/ | ||
export const converge = (...params) => { | ||
// @signature (accFn, extractFn) => (source) | ||
if (params.length <= 2) { | ||
return source => _converge(params[0], params[1], source) | ||
} | ||
|
||
// @signature (accFn, extractFn, source) | ||
return _converge(...params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import test from "tape" | ||
import { converge, reduce, read } from ".." | ||
|
||
test("converge", t => { | ||
const obj = { a: 1, b: 2 } | ||
const sum = (...params) => reduce((acc, item) => acc + item, 0, params) | ||
|
||
t.equals( | ||
converge(sum, [read("a"), read("b")])(obj), | ||
3, | ||
"Extract object properties + sum (curried)" | ||
) | ||
|
||
t.equals( | ||
converge(sum, read("b"), obj), | ||
2, | ||
"Extract object property + sum (uncurried)" | ||
) | ||
|
||
t.equals( | ||
converge([sum], [read("a"), read("b")], obj), | ||
3, | ||
"Extract object properties + sum (uncurried)" | ||
) | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters