Skip to content

Commit b0b2102

Browse files
jethrolarsonhemanth
authored andcommitted
Update definition of partial application (hemanth#75)
1 parent ab7d286 commit b0b2102

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

readme.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,35 @@ filter(is(Number), [0, '1', 2, null]); // [0, 2]
8686

8787
## Partial Application
8888

89-
The process of getting a function with lesser arity compared to the original
90-
function by fixing the number of arguments is known as partial application.
89+
Partially applying a function means creating a new function by pre-filling some of the arguments to the original function.
90+
9191

9292
```js
93-
let sum = (a, b) => a + b;
93+
// Helper to create partially applied functions
94+
// Takes a function and some arguments
95+
let partial = (f, ...args) =>
96+
// returns a function that takes the rest of the arguments
97+
(...moreArgs) =>
98+
// and calls the original function with all of them
99+
f(...[...args, ...moreArgs]);
100+
101+
// Something to apply
102+
let add3 = (a, b, c) => a + b + c;
94103

95-
// partially applying `a` to `40`
96-
let partial = sum.bind(null, 40);
104+
// Partially applying `2` and `3` to `add3` gives you a one-argument function
105+
const fivePlus = partial(add3, 2, 3); // (c) => 2 + 3 + c
97106

98-
// Invoking it with `b`
99-
partial(2); // 42
107+
fivePlus(4); // 9
100108
```
101109

110+
You can also use `Function.prototype.bind` to partially apply a function in JS:
111+
112+
```js
113+
const add1More = add3.bind(null, 2, 3); // (c) => 2 + 3 + c
114+
```
115+
116+
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.
117+
102118
## Currying
103119

104120
The process of converting a function that takes multiple arguments into a function that takes them one at a time.

0 commit comments

Comments
 (0)