diff --git a/snippets/converge.md b/snippets/converge.md new file mode 100644 index 000000000..511a5e2d9 --- /dev/null +++ b/snippets/converge.md @@ -0,0 +1,19 @@ +### converge + +Accepts a converging function and a list of branching functions and returns a function that applies each branching function to the arguments and the results of the branching functions are passed as arguments to the converging function. + +Use `Array.map()` and `Function.apply()` to apply each function to the given arguments. +Use the spread operator (`...`) to call `coverger` with the results of all other functions. + +```js +const converge = (converger, fns) => (...args) => + converger(...fns.map(fn => fn.apply(null, args))); +``` + +```js +const average = converge((a, b) => a / b, [ + arr => arr.reduce((a, v) => a + v, 0), + arr => arr.length, +]); +average([1, 2, 3, 4, 5, 6, 7]); // 4 +``` diff --git a/tag_database b/tag_database index b35d6b5ce..43581b8cb 100644 --- a/tag_database +++ b/tag_database @@ -26,6 +26,7 @@ colorize:node,utility,string compact:array compose:function composeRight:function +converge:function copyToClipboard:browser,string,advanced countBy:array,object countOccurrences:array diff --git a/test/converge/converge.js b/test/converge/converge.js new file mode 100644 index 000000000..546dd65b6 --- /dev/null +++ b/test/converge/converge.js @@ -0,0 +1,3 @@ +const converge = (converger, fns) => (...args) => +converger(...fns.map(fn => fn.apply(null, args))); +module.exports = converge; \ No newline at end of file diff --git a/test/converge/converge.test.js b/test/converge/converge.test.js new file mode 100644 index 000000000..73cafbf11 --- /dev/null +++ b/test/converge/converge.test.js @@ -0,0 +1,23 @@ +const test = require('tape'); +const converge = require('./converge.js'); + +test('Testing converge', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof converge === 'function', 'converge is a Function'); + const average = converge((a, b) => a / b, [ + arr => arr.reduce((a, v) => a + v, 0), + arr => arr.length, + ]); + t.equal(average([1, 2, 3, 4, 5, 6, 7]), 4, 'Produces the average of the array'); + const strangeConcat = converge((a, b) => a + b, [ + x => x.toUpperCase(), + x => x.toLowerCase()] + ); + t.equal(strangeConcat('Yodel'), "YODELyodel", 'Produces the strange concatenation'); + //t.deepEqual(converge(args..), 'Expected'); + //t.equal(converge(args..), 'Expected'); + //t.false(converge(args..), 'Expected'); + //t.throws(converge(args..), 'Expected'); + t.end(); +}); diff --git a/test/testlog b/test/testlog index 2d454e050..e7ce95fc5 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Wed Feb 07 2018 11:34:35 GMT+0200 (GTB Standard Time) +Test log for: Wed Feb 07 2018 12:22:33 GMT+0200 (GTB Standard Time) > 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -190,6 +190,12 @@ Test log for: Wed Feb 07 2018 11:34:35 GMT+0200 (GTB Standard Time) √ composeRight is a Function √ Performs left-to-right function composition + Testing converge + + √ converge is a Function + √ Produces the average of the array + √ Produces the strange concatenation + Testing copyToClipboard √ copyToClipboard is a Function @@ -1319,7 +1325,6 @@ Test log for: Wed Feb 07 2018 11:34:35 GMT+0200 (GTB Standard Time) Testing shuffle √ shuffle is a Function - √ Shuffles the array Testing similarity @@ -1741,8 +1746,8 @@ Test log for: Wed Feb 07 2018 11:34:35 GMT+0200 (GTB Standard Time) √ Works with multiple promises - total: 842 - passing: 842 + total: 844 + passing: 844 duration: 2.4s