Skip to content

Commit 222c52f

Browse files
committedApr 10, 2019
minor
1 parent 4f925a1 commit 222c52f

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed
 

‎1-js/06-advanced-functions/11-currying-partials/article.md

+19-18
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,11 @@ Also there's a ready [_.partial](https://lodash.com/docs#partial) implementation
119119

120120
Sometimes people mix up partial function application mentioned above with another thing named "currying". That's another interesting technique of working with functions that we just have to mention here.
121121

122-
[Currying](https://en.wikipedia.org/wiki/Currying) is translating a function from callable as `f(a, b, c)` into callable as `f(a)(b)(c)`.
122+
[Currying](https://en.wikipedia.org/wiki/Currying) is a transformation of functions that translates a function from callable as `f(a, b, c)` into callable as `f(a)(b)(c)`. In JavaScript, we usually make a wrapper to keep the original function.
123123

124-
Literally, currying is a transformation of functions: from one way of calling into another. In JavaScript, we usually make a wrapper to keep the original function.
124+
Currying doesn't call a function. It just transforms it.
125125

126-
Currying doesn't call a function. It just transforms it. We'll see use cases soon.
127-
128-
Let's make `curry` function that performs currying for two-argument functions. In other words, `curry(f)` for two-argument `f(a, b)` translates it into `f(a)(b)`
126+
Let's create a helper `curry(f)` function that performs currying for a two-argument `f`. In other words, `curry(f)` for two-argument `f(a, b)` translates it into `f(a)(b)`
129127

130128
```js run
131129
*!*
@@ -168,9 +166,11 @@ function curry(f) {
168166

169167
## Currying? What for?
170168

171-
To understand the benefits we definitely need a worthy real-life example. Advanced currying allows the function to be both callable normally and get partials.
169+
To understand the benefits we definitely need a worthy real-life example.
170+
171+
Advanced currying allows the function to be both callable normally and partially.
172172

173-
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 it over the network or filtering:
173+
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:
174174

175175
```js
176176
function log(date, importance, message) {
@@ -216,13 +216,15 @@ todayDebug("message"); // [HH:mm] DEBUG message
216216

217217
So:
218218
1. We didn't lose anything after currying: `log` is still callable normally.
219-
2. We were able to generate partial functions that are convenient in many cases.
219+
2. We were able to generate partial functions such as for today's logs.
220220

221221
## Advanced curry implementation
222222

223-
In case you're interested, here's the "advanced" curry implementation that we could use above, it's pretty short:
223+
In case you'd like to get in details (not obligatory!), here's the "advanced" curry implementation that we could use above.
224224

225-
```js run
225+
It's pretty short:
226+
227+
```js
226228
function curry(func) {
227229

228230
return function curried(...args) {
@@ -236,21 +238,20 @@ function curry(func) {
236238
};
237239

238240
}
241+
```
242+
243+
Usage examples:
239244

245+
```js
240246
function sum(a, b, c) {
241247
return a + b + c;
242248
}
243249

244250
let curriedSum = curry(sum);
245251

246-
// still callable normally
247-
alert( curriedSum(1, 2, 3) ); // 6
248-
249-
// get the partial with curried(1) and call it with 2 other arguments
250-
alert( curriedSum(1)(2,3) ); // 6
251-
252-
// full curried form
253-
alert( curriedSum(1)(2)(3) ); // 6
252+
alert( curriedSum(1, 2, 3) ); // 6, still callable normally
253+
alert( curriedSum(1)(2,3) ); // 6, currying of 1st arg
254+
alert( curriedSum(1)(2)(3) ); // 6, full currying
254255
```
255256

256257
The new `curry` may look complicated, but it's actually easy to understand.

‎todo.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
- History api
44
- Pointer events
55
- Touch events
6+
- Canvas (2d graphics)
67
- Security (xsrf xss csp etc)

0 commit comments

Comments
 (0)
Please sign in to comment.