diff --git a/README.md b/README.md index c0e57d210..6adbe5366 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,15 @@ average(1, 2, 3); +### Adaptor + +
+View contents + +* [`unary`](#unary) + +
+ ### 📚 Array
@@ -586,6 +595,30 @@ arrayMax([1, 2, 3]); // 3
[⬆ Back to top](#table-of-contents) +--- + ## Adaptor + +### unary + +Creates a function that accepts up to one argument, ignoring any additional arguments. + +Call the provided function, `fn`, with just the first argument given. + +```js +const unary = fn => val => fn(val); +``` + +
+Examples + +```js +['6', '8', '10'].map(unary(parseInt)); // [6, 8, 10] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + --- ## 📚 Array diff --git a/docs/index.html b/docs/index.html index f4b41a2a5..a621c02f4 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

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

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 ]
@@ -89,6 +89,8 @@ Object.assig
 

spreadOver

Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.

Use closures and the spread operator (...) to map the array of arguments to the inputs of the function.

const spreadOver = fn => argsArr => fn(...argsArr);
 
const arrayMax = spreadOver(Math.max);
 arrayMax([1, 2, 3]); // 3
+

Adaptor

unary

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

Call the provided function, fn, with just the first argument given.

const unary = fn => val => fn(val);
+
['6', '8', '10'].map(unary(parseInt)); // [6, 8, 10]
 

Array

chunk

Chunks an array into smaller arrays of a specified size.

Use Array.from() to create a new array, that fits the number of chunks that will be produced. Use Array.slice() to map each element of the new array to a chunk the length of size. If the original array can't be split evenly, the final chunk will contain the remaining elements.

const chunk = (arr, size) =>
   Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
     arr.slice(i * size, i * size + size)