@ -1,6 +1,6 @@
|
||||
---
|
||||
title: ary
|
||||
tags: adapter,function,intermediate
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Creates a function that accepts up to `n` arguments, ignoring any additional arguments.
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
---
|
||||
title: call
|
||||
tags: adapter,function,intermediate
|
||||
---
|
||||
|
||||
---
|
||||
title: call
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```js
|
||||
const call = (key, ...args) => context => context[key](...args);
|
||||
```
|
||||
|
||||
```js
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(call('map', x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
const map = call.bind(null, 'map');
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(map(x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
```
|
||||
Use a closure to call a stored key with stored arguments.
|
||||
|
||||
```js
|
||||
const call = (key, ...args) => context => context[key](...args);
|
||||
```
|
||||
|
||||
```js
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(call('map', x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
const map = call.bind(null, 'map');
|
||||
Promise.resolve([1, 2, 3])
|
||||
.then(map(x => 2 * x))
|
||||
.then(console.log); // [ 2, 4, 6 ]
|
||||
```
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
---
|
||||
title: collectInto
|
||||
tags: adapter,function,array,intermediate
|
||||
---
|
||||
|
||||
---
|
||||
title: collectInto
|
||||
tags: function,array,intermediate
|
||||
---
|
||||
|
||||
Changes a function that accepts an array into a variadic function.
|
||||
|
||||
Given a function, return a closure that collects all inputs into an array-accepting function.
|
||||
|
||||
```js
|
||||
const collectInto = fn => (...args) => fn(args);
|
||||
```
|
||||
|
||||
```js
|
||||
const Pall = collectInto(Promise.all.bind(Promise));
|
||||
let p1 = Promise.resolve(1);
|
||||
let p2 = Promise.resolve(2);
|
||||
let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3));
|
||||
Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds)
|
||||
```
|
||||
Given a function, return a closure that collects all inputs into an array-accepting function.
|
||||
|
||||
```js
|
||||
const collectInto = fn => (...args) => fn(args);
|
||||
```
|
||||
|
||||
```js
|
||||
const Pall = collectInto(Promise.all.bind(Promise));
|
||||
let p1 = Promise.resolve(1);
|
||||
let p2 = Promise.resolve(2);
|
||||
let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3));
|
||||
Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds)
|
||||
```
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
---
|
||||
title: flip
|
||||
tags: adapter,function,intermediate
|
||||
---
|
||||
|
||||
---
|
||||
title: flip
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Flip takes a function as an argument, then makes the first argument the last.
|
||||
|
||||
Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
|
||||
|
||||
```js
|
||||
const flip = fn => (first, ...rest) => fn(...rest, first);
|
||||
```
|
||||
|
||||
```js
|
||||
let a = { name: 'John Smith' };
|
||||
let b = {};
|
||||
const mergeFrom = flip(Object.assign);
|
||||
let mergePerson = mergeFrom.bind(null, a);
|
||||
mergePerson(b); // == b
|
||||
b = {};
|
||||
Object.assign(b, a); // == b
|
||||
```
|
||||
Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
|
||||
|
||||
```js
|
||||
const flip = fn => (first, ...rest) => fn(...rest, first);
|
||||
```
|
||||
|
||||
```js
|
||||
let a = { name: 'John Smith' };
|
||||
let b = {};
|
||||
const mergeFrom = flip(Object.assign);
|
||||
let mergePerson = mergeFrom.bind(null, a);
|
||||
mergePerson(b); // == b
|
||||
b = {};
|
||||
Object.assign(b, a); // == b
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: over
|
||||
tags: adapter,function,intermediate
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Creates a function that invokes each provided function with the arguments it receives and returns the results.
|
||||
@ -14,4 +14,4 @@ const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args));
|
||||
```js
|
||||
const minMax = over(Math.min, Math.max);
|
||||
minMax(1, 2, 3, 4, 5); // [1,5]
|
||||
```
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: overArgs
|
||||
tags: adapter,function,intermediate
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Creates a function that invokes the provided function with its arguments transformed.
|
||||
@ -16,4 +16,4 @@ const square = n => n * n;
|
||||
const double = n => n * 2;
|
||||
const fn = overArgs((x, y) => [x, y], [square, double]);
|
||||
fn(9, 3); // [81, 6]
|
||||
```
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: pipeAsyncFunctions
|
||||
tags: adapter,function,promise,intermediate
|
||||
tags: function,promise,intermediate
|
||||
---
|
||||
|
||||
Performs left-to-right function composition for asynchronous functions.
|
||||
@ -23,4 +23,4 @@ const sum = pipeAsyncFunctions(
|
||||
(async () => {
|
||||
console.log(await sum(5)); // 15 (after one second)
|
||||
})();
|
||||
```
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: pipeFunctions
|
||||
tags: adapter,function,intermediate
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Performs left-to-right function composition.
|
||||
@ -17,4 +17,4 @@ const add5 = x => x + 5;
|
||||
const multiply = (x, y) => x * y;
|
||||
const multiplyAndAdd5 = pipeFunctions(multiply, add5);
|
||||
multiplyAndAdd5(5, 2); // 15
|
||||
```
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: promisify
|
||||
tags: adapter,function,promise,intermediate
|
||||
tags: function,promise,intermediate
|
||||
---
|
||||
|
||||
Converts an asynchronous function to return a promise.
|
||||
@ -20,4 +20,4 @@ const promisify = func => (...args) =>
|
||||
```js
|
||||
const delay = promisify((d, cb) => setTimeout(cb, d));
|
||||
delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s
|
||||
```
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: rearg
|
||||
tags: adapter,function,intermediate
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.
|
||||
@ -19,4 +19,4 @@ var rearged = rearg(
|
||||
[2, 0, 1]
|
||||
);
|
||||
rearged('b', 'c', 'a'); // ['a', 'b', 'c']
|
||||
```
|
||||
```
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
---
|
||||
title: spreadOver
|
||||
tags: adapter,intermediate
|
||||
---
|
||||
|
||||
---
|
||||
title: spreadOver
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```js
|
||||
const spreadOver = fn => argsArr => fn(...argsArr);
|
||||
```
|
||||
|
||||
```js
|
||||
const arrayMax = spreadOver(Math.max);
|
||||
arrayMax([1, 2, 3]); // 3
|
||||
```
|
||||
Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function.
|
||||
|
||||
```js
|
||||
const spreadOver = fn => argsArr => fn(...argsArr);
|
||||
```
|
||||
|
||||
```js
|
||||
const arrayMax = spreadOver(Math.max);
|
||||
arrayMax([1, 2, 3]); // 3
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: unary
|
||||
tags: adapter,function,intermediate
|
||||
tags: function,beginner
|
||||
---
|
||||
|
||||
Creates a function that accepts up to one argument, ignoring any additional arguments.
|
||||
@ -13,4 +13,4 @@ const unary = fn => val => fn(val);
|
||||
|
||||
```js
|
||||
['6', '8', '10'].map(unary(parseInt)); // [6, 8, 10]
|
||||
```
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user