Merge pull request #382 from brentguf/currying
Currying/Partials chapter
This commit is contained in:
commit
228873c901
2 changed files with 3 additions and 3 deletions
|
@ -8,7 +8,7 @@ The task is a little more complex variant of <info:task/question-use-bind>.
|
||||||
|
|
||||||
The `user` object was modified. Now instead of two functions `loginOk/loginFail`, it has a single function `user.login(true/false)`.
|
The `user` object was modified. Now instead of two functions `loginOk/loginFail`, it has a single function `user.login(true/false)`.
|
||||||
|
|
||||||
What to pass `askPassword` in the code below, so that it calls `user.login(true)` as `ok` and `user.login(fail)` as `fail`?
|
What to pass `askPassword` in the code below, so that it calls `user.login(true)` as `ok` and `user.login(false)` as `fail`?
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function askPassword(ok, fail) {
|
function askPassword(ok, fail) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ libs:
|
||||||
|
|
||||||
# Currying and partials
|
# Currying and partials
|
||||||
|
|
||||||
Till now we were only talking about binding `this`. Now let's make a step further.
|
Until now we have only been talking about binding `this`. Let's take it a step further.
|
||||||
|
|
||||||
We can bind not only `this`, but also arguments. That's rarely done, but sometimes can be handy.
|
We can bind not only `this`, but also arguments. That's rarely done, but sometimes can be handy.
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ More advanced implementations of currying like [_.curry](https://lodash.com/docs
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function curry(f) {
|
function curry(f) {
|
||||||
return function(..args) {
|
return function(...args) {
|
||||||
// if args.length == f.length (as many arguments as f has),
|
// if args.length == f.length (as many arguments as f has),
|
||||||
// then pass the call to f
|
// then pass the call to f
|
||||||
// otherwise return a partial function that fixes args as first arguments
|
// otherwise return a partial function that fixes args as first arguments
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue