You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/99-js-misc/03-currying-partials/article.md
+76-76Lines changed: 76 additions & 76 deletions
Original file line number
Diff line number
Diff line change
@@ -3,21 +3,21 @@ libs:
3
3
4
4
---
5
5
6
-
# Currying
6
+
# Curryování
7
7
8
-
[Currying](https://en.wikipedia.org/wiki/Currying)is an advanced technique of working with functions. It's used not only in JavaScript, but in other languages as well.
8
+
[Curryování neboli currying](https://en.wikipedia.org/wiki/Currying)je pokročilá technika práce s funkcemi. Používá se nejen v JavaScriptu, ale i v jiných jazycích.
9
9
10
-
Currying is a transformation of functions that translates a function from callable as `f(a, b, c)`into callable as`f(a)(b)(c)`.
10
+
Curryování je transformace funkce, která přeloží funkci volatelnou způsobem `f(a, b, c)`na funkci volatelnou způsobem`f(a)(b)(c)`.
11
11
12
-
Currying doesn't call a function. It just transforms it.
12
+
Curryování nevolá funkci, jen ji transformuje.
13
13
14
-
Let's see an example first, to better understand what we're talking about, and then practical applications.
14
+
Nejprve se podívejme na příklad, abychom lépe porozuměli tomu, o čem se tady mluví, a pak na praktické aplikace.
15
15
16
-
We'll create a helper function `curry(f)` that performs currying for a two-argument `f`. In other words, `curry(f)`for two-argument `f(a, b)`translates it into a function that runs as`f(a)(b)`:
16
+
Vytvořme pomocnou funkci `curry(f)`, která provádí curryování dvouargumentové funkce `f`. Jinými slovy, `curry(f)`na dvouargumentové funkci `f(a, b)`ji přeloží na funkci, která se bude spouštět jako`f(a)(b)`:
17
17
18
18
```js run
19
19
*!*
20
-
functioncurry(f) { // curry(f) does the currying transform
@@ -26,163 +26,163 @@ function curry(f) { // curry(f) does the currying transform
26
26
}
27
27
*/!*
28
28
29
-
//usage
30
-
functionsum(a, b) {
29
+
//použití
30
+
functionsoučet(a, b) {
31
31
return a + b;
32
32
}
33
33
34
-
letcurriedSum=curry(sum);
34
+
letcurryovanýSoučet=curry(součet);
35
35
36
-
alert( curriedSum(1)(2) ); // 3
36
+
alert( curryovanýSoučet(1)(2) ); // 3
37
37
```
38
38
39
-
As you can see, the implementation is straightforward: it's just two wrappers.
39
+
Jak vidíte, implementace je přímočará: jsou to pouhé dva obaly.
40
40
41
-
-The result of `curry(func)`is a wrapper`function(a)`.
42
-
-When it is called like `curriedSum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned`function(b)`.
43
-
-Then this wrapper is called with `2`as an argument, and it passes the call to the original `sum`.
41
+
-Výsledkem `curry(func)`je obal`function(a)`.
42
+
-Když je zavolán způsobem `curryovanýSoučet(1)`, argument se uloží do lexikálního prostředí a vrátí se nový obal`function(b)`.
43
+
-Pak je tento obal volán s argumentem `2`a předá volání původní funkci `součet`.
44
44
45
-
More advanced implementations of currying, such as [_.curry](https://lodash.com/docs#curry)from lodash library, return a wrapper that allows a function to be called both normally and partially:
45
+
Pokročilejší implementace curryování, např. [_.curry](https://lodash.com/docs#curry)z knihovny lodash, vrátí obal, který umožní volat funkci jak obvyklým způsobem, tak parciálně:
46
46
47
47
```js run
48
-
functionsum(a, b) {
48
+
functionsoučet(a, b) {
49
49
return a + b;
50
50
}
51
51
52
-
letcurriedSum=_.curry(sum); //using _.curry from lodash library
52
+
letcurryovanýSoučet=_.curry(součet); //použijeme _.curry z knihovny lodash
53
53
54
-
alert( curriedSum(1, 2) ); // 3, still callable normally
55
-
alert( curriedSum(1)(2) ); // 3, called partially
54
+
alert( curryovanýSoučet(1, 2) ); // 3, stále volatelná normálně
To understand the benefits we need a worthy real-life example.
60
+
Abychom pochopili výhody, potřebujeme cenný příklad z reálného života.
61
61
62
-
For instance, we have the logging function `log(date, importance, message)` that formats and outputs the information. In real projects such functions have many useful features like sending logs over the network, here we'll just use`alert`:
62
+
Mějme například logovací funkci `log(datum, důležitost, zpráva)`, která naformátuje a vypíše zadané informace. Ve skutečných projektech mají takové funkce mnoho užitečných možností, např. posílání logů po síti, zde jenom zavoláme`alert`:
The new`curry`may look complicated, but it's actually easy to understand.
148
+
Nová funkce`curry`může vypadat komplikovaně, ale ve skutečnosti je snadné jí porozumět.
149
149
150
-
The result of `curry(func)`call is the wrapper `curried` that looks like this:
150
+
Výsledkem volání `curry(funkce)`je obal `curryovaná`, který vypadá takto:
151
151
152
152
```js
153
-
//func is the function to transform
154
-
functioncurried(...args) {
155
-
if (args.length>=func.length) { // (1)
156
-
returnfunc.apply(this, args);
153
+
//funkce je funkce, která se má transformovat
154
+
functioncurryovaná(...args) {
155
+
if (args.length>=funkce.length) { // (1)
156
+
returnfunkce.apply(this, args);
157
157
} else {
158
158
returnfunction(...args2) { // (2)
159
-
returncurried.apply(this, args.concat(args2));
159
+
returncurryovaná.apply(this, args.concat(args2));
160
160
}
161
161
}
162
162
};
163
163
```
164
164
165
-
When we run it, there are two `if` execution branches:
165
+
Když ji spustíme, obsahuje dvě běhové větve `if`:
166
166
167
-
1.If passed `args`count is the same or more than the original function has in its definition (`func.length`) , then just pass the call to it using `func.apply`.
168
-
2.Otherwise, get a partial: we don't call `func` just yet. Instead, another wrapper is returned, that will re-apply `curried` providing previous arguments together with the new ones.
167
+
1.Je-li počet předaných argumentů `args`stejný nebo větší, než v definici původní funkce (`funkce.length`), pak jí jen předáme volání pomocí `funkce.apply`.
168
+
2.V opačném případě získáme parciální funkci: ještě funkci `funkce` nebudeme volat. Místo toho se vrátí další obal, který znovu aplikuje funkci `curryovaná` a poskytne jí předchozí argumenty společně s novými.
169
169
170
-
Then, if we call it, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
170
+
Když ji pak znovu zavoláme, získáme buď novou parciální funkci (nemáme-li ještě dost argumentů), nebo nakonec výsledek.
171
171
172
-
```smart header="Fixed-length functions only"
173
-
The currying requires the function to have a fixed number of arguments.
172
+
```smart header="Jen pro funkce s pevnou délkou"
173
+
Curryování vyžaduje, aby funkce měla pevný počet argumentů.
174
174
175
-
A function that uses rest parameters, such as `f(...args)`, can't be curried this way.
175
+
Funkci, která používá zbytkové parametry, např. `f(...args)`, nelze tímto způsobem curryovat.
176
176
```
177
177
178
-
```smart header="A little more than currying"
179
-
By definition, currying should convert `sum(a, b, c)` into `sum(a)(b)(c)`.
178
+
```smart header="Víc než jen curryování"
179
+
Podle definice by curryování mělo převést `součet(a, b, c)` na `součet(a)(b)(c)`.
180
180
181
-
But most implementations of currying in JavaScript are advanced, as described: they also keep the function callable in the multi-argument variant.
181
+
Většina implementací curryování v JavaScriptu je však pokročilá, jak bylo uvedeno: udržují funkci volatelnou i ve víceargumentové variantě.
182
182
```
183
183
184
-
## Summary
184
+
## Shrnutí
185
185
186
-
*Currying* is a transform that makes`f(a,b,c)`callable as `f(a)(b)(c)`. JavaScript implementations usually both keep the function callable normally and return the partial if the arguments count is not enough.
186
+
*Curryování* je transformace, která umožní volat`f(a,b,c)`jako `f(a)(b)(c)`. Implementace v JavaScriptu obvykle současně ponechají funkci volatelnou normálně, ale není-li poskytnuto dost argumentů, vrátí parciální funkci.
187
187
188
-
Currying allows us to easily get partials. As we've seen in the logging example, after currying the three argument universal function `log(date, importance, message)`gives us partials when called with one argument (like`log(date)`) or two arguments (like`log(date, importance)`).
188
+
Curryování nám umožní snadno získat parciální funkci. Jak jsme viděli v příkladu s logováním, univerzální tříargumentová funkce `log(datum, důležitost, zpráva)`nám po curryování vydá parciální funkci, když je volána s jedním argumentem (např.`log(datum)`) nebo se dvěma argumenty (např.`log(datum, důležitost)`).
0 commit comments