diff --git a/README.md b/README.md index a197fc793..dae4b557a 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ average(1, 2, 3);
View contents +* [`ary`](#ary) * [`call`](#call) * [`collectInto`](#collectinto) * [`flip`](#flip) @@ -414,6 +415,29 @@ average(1, 2, 3); --- ## 🔌 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 (`...`). + +```js +const ary = (fn, n) => (...args) => fn(...args.slice(0, n)); +``` + +
+Examples + +```js +const firstTwoMax = ary(Math.max, 2); +[[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### call Given a key and a set of arguments, call them when given a context. Primarily useful in composition. diff --git a/docs/index.html b/docs/index.html index 4a19d1625..db0ed852a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,10 @@ scrollToTop(); } }, false); - }

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

 

Adapter

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);
+      }

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);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]