Travis build: 392
This commit is contained in:
@ -5,15 +5,15 @@ Given a key and a set of arguments, call them when given a context. Primarily us
|
||||
Use a closure to call a stored key with stored arguments.
|
||||
|
||||
```js
|
||||
const call = (key, ...args) => context => context[key](...args);
|
||||
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 ]
|
||||
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 ]
|
||||
```
|
||||
|
||||
@ -5,13 +5,13 @@ 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);
|
||||
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);
|
||||
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);
|
||||
```
|
||||
|
||||
@ -5,15 +5,15 @@ 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 => (...args) => fn(args.pop(), ...args);
|
||||
const flip = fn => (...args) => fn(args.pop(), ...args);
|
||||
```
|
||||
|
||||
```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
|
||||
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
|
||||
```
|
||||
|
||||
@ -5,11 +5,11 @@ Takes a variadic function and returns a closure that accepts an array of argumen
|
||||
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);
|
||||
const spreadOver = fn => argsArr => fn(...argsArr);
|
||||
```
|
||||
|
||||
```js
|
||||
const arrayMax = spreadOver(Math.max);
|
||||
arrayMax([1, 2, 3]); // 3
|
||||
arrayMax([1, 2, 4]); // 4
|
||||
const arrayMax = spreadOver(Math.max);
|
||||
arrayMax([1, 2, 3]); // 3
|
||||
arrayMax([1, 2, 4]); // 4
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user