19
snippets/converge.md
Normal file
19
snippets/converge.md
Normal file
@ -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
|
||||
```
|
||||
@ -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
|
||||
|
||||
3
test/converge/converge.js
Normal file
3
test/converge/converge.js
Normal file
@ -0,0 +1,3 @@
|
||||
const converge = (converger, fns) => (...args) =>
|
||||
converger(...fns.map(fn => fn.apply(null, args)));
|
||||
module.exports = converge;
|
||||
23
test/converge/converge.test.js
Normal file
23
test/converge/converge.test.js
Normal file
@ -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();
|
||||
});
|
||||
13
test/testlog
13
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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user