From 2b2e88738c7b21c9092df7d3ce2191cab951b68d Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 7 Feb 2018 10:24:32 +0000 Subject: [PATCH] Travis build: 1584 --- README.md | 28 ++++++++++++++++++++++++++++ docs/index.html | 8 +++++++- snippets/converge.md | 5 ++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0a640e34a..edadf62e6 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,7 @@ average(1, 2, 3); * [`chainAsync`](#chainasync) * [`compose`](#compose) * [`composeRight`](#composeright) +* [`converge`](#converge) * [`curry`](#curry) * [`debounce`](#debounce) * [`defer`](#defer) @@ -3766,6 +3767,33 @@ addAndSquare(1, 2); // 9
[⬆ Back to top](#table-of-contents) +### 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))); +``` + +
+Examples + +```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 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### curry Curries a function. diff --git a/docs/index.html b/docs/index.html index 54ff3a766..5011e05cf 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -848,6 +848,12 @@ console.log<
 const square = x => x * x;
 const addAndSquare = composeRight(add, square);
 addAndSquare(1, 2); // 9
+

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.

const converge = (converger, fns) => (...args) => converger(...fns.map(fn => fn.apply(null, args)));
+
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
 

curry

Curries a function.

Use recursion. If the number of provided arguments (args) is sufficient, call the passed function fn. Otherwise, return a curried function fn that expects the rest of the arguments. If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. Math.min()), you can optionally pass the number of arguments to the second parameter arity.

const curry = (fn, arity = fn.length, ...args) =>
   arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
 
curry(Math.pow)(2)(10); // 1024
diff --git a/snippets/converge.md b/snippets/converge.md
index 511a5e2d9..a3912d391 100644
--- a/snippets/converge.md
+++ b/snippets/converge.md
@@ -6,14 +6,13 @@ Use `Array.map()` and `Function.apply()` to apply each function to the given arg
 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)));
+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,
+  arr => arr.length
 ]);
 average([1, 2, 3, 4, 5, 6, 7]); // 4
 ```