This commit is contained in:
Ilya Kantor 2019-06-08 11:40:47 +03:00
parent 3c37e1de7b
commit f1bf4f12c9
4 changed files with 52 additions and 61 deletions

View file

@ -170,7 +170,7 @@ To understand the benefits we definitely need a worthy real-life example.
Advanced currying allows the function to be both callable normally and partially.
For instance, we have the logging function `log(date, importance, message)` that formats and outputs the information. In real projects such functions also have many other useful features like sending logs over the network:
For instance, we have the logging function `log(date, importance, message)` that formats and outputs the information. In real projects such functions also have many other useful features like sending logs over the network, here we just use `alert`:
```js
function log(date, importance, message) {
@ -184,34 +184,29 @@ Let's curry it!
log = _.curry(log);
```
After that `log` still works the normal way:
```js
log(new Date(), "DEBUG", "some debug");
```
...But also can be called in the curried form:
After that `log` work both the normal way and in the curried form:
```js
log(new Date(), "DEBUG", "some debug"); // log(a,b,c)
log(new Date())("DEBUG")("some debug"); // log(a)(b)(c)
```
Let's get a convenience function for today's logs:
Now we can easily make a convenience function for current logs:
```js
// todayLog will be the partial of log with fixed first argument
let todayLog = log(new Date());
// currentLog will be the partial of log with fixed first argument
let currentLog = log(new Date());
// use it
todayLog("INFO", "message"); // [HH:mm] INFO message
currentLog("INFO", "message"); // [HH:mm] INFO message
```
And now a convenience function for today's debug messages:
And here's a convenience function for current debug messages:
```js
let todayDebug = todayLog("DEBUG");
let todayDebug = currentLog("DEBUG");
todayDebug("message"); // [HH:mm] DEBUG message
currentLog("message"); // [HH:mm] DEBUG message
```
So: