Add partial, partialRight

This commit is contained in:
Angelos Chalaris
2018-01-24 14:40:16 +02:00
parent ec38d84eec
commit e4d6368ecb
3 changed files with 36 additions and 0 deletions

17
snippets/partial.md Normal file
View 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
View 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!'
```