Add partial, partialRight
This commit is contained in:
17
snippets/partial.md
Normal file
17
snippets/partial.md
Normal file
@ -0,0 +1,17 @@
|
||||
### partial
|
||||
|
||||
Creates a function that invokes `fn` with `partials` prepended to the arguments it receives.
|
||||
|
||||
Use the spread operator (`...`) to prepend `partials` to the list of arguments of `fn`.
|
||||
|
||||
```js
|
||||
const partial = (fn, ...partials) => (...args) => fn(...partials, ...args);
|
||||
```
|
||||
|
||||
```js
|
||||
function greet(greeting, name) {
|
||||
return greeting + ' ' + name + '!';
|
||||
}
|
||||
const greetHello = partial(greet, 'Hello');
|
||||
greetHello('John'); // 'Hello John!'
|
||||
```
|
||||
17
snippets/partialRight.md
Normal file
17
snippets/partialRight.md
Normal file
@ -0,0 +1,17 @@
|
||||
### partialRight
|
||||
|
||||
Creates a function that invokes `fn` with `partials` appended to the arguments it receives.
|
||||
|
||||
Use the spread operator (`...`) to append `partials` to the list of arguments of `fn`.
|
||||
|
||||
```js
|
||||
const partialRight = (fn, ...partials) => (...args) => fn( ...args, ...partials);
|
||||
```
|
||||
|
||||
```js
|
||||
function greet(greeting, name) {
|
||||
return greeting + ' ' + name + '!';
|
||||
}
|
||||
const greetJohn = partialRight(greet, 'John');
|
||||
greetJohn('Hello'); // 'Hello John!'
|
||||
```
|
||||
Reference in New Issue
Block a user