mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-07 02:56:43 +00:00
Update definition of partial application (#75)
This commit is contained in:
parent
ab7d286329
commit
b0b210248c
1 changed files with 23 additions and 7 deletions
30
readme.md
30
readme.md
|
|
@ -86,19 +86,35 @@ filter(is(Number), [0, '1', 2, null]); // [0, 2]
|
|||
|
||||
## Partial Application
|
||||
|
||||
The process of getting a function with lesser arity compared to the original
|
||||
function by fixing the number of arguments is known as partial application.
|
||||
Partially applying a function means creating a new function by pre-filling some of the arguments to the original function.
|
||||
|
||||
|
||||
```js
|
||||
let sum = (a, b) => a + b;
|
||||
// Helper to create partially applied functions
|
||||
// Takes a function and some arguments
|
||||
let partial = (f, ...args) =>
|
||||
// returns a function that takes the rest of the arguments
|
||||
(...moreArgs) =>
|
||||
// and calls the original function with all of them
|
||||
f(...[...args, ...moreArgs]);
|
||||
|
||||
// partially applying `a` to `40`
|
||||
let partial = sum.bind(null, 40);
|
||||
// Something to apply
|
||||
let add3 = (a, b, c) => a + b + c;
|
||||
|
||||
// Invoking it with `b`
|
||||
partial(2); // 42
|
||||
// Partially applying `2` and `3` to `add3` gives you a one-argument function
|
||||
const fivePlus = partial(add3, 2, 3); // (c) => 2 + 3 + c
|
||||
|
||||
fivePlus(4); // 9
|
||||
```
|
||||
|
||||
You can also use `Function.prototype.bind` to partially apply a function in JS:
|
||||
|
||||
```js
|
||||
const add1More = add3.bind(null, 2, 3); // (c) => 2 + 3 + c
|
||||
```
|
||||
|
||||
Partial application helps create simpler functions from more complex ones by baking in data when you have it. [Curried](#currying) functions are automatically partially applied.
|
||||
|
||||
## Currying
|
||||
|
||||
The process of converting a function that takes multiple arguments into a function that takes them one at a time.
|
||||
|
|
|
|||
Loading…
Reference in a new issue