Skip to content

Commit 8dc26c7

Browse files
committed
Merge branch '1.14.3' of https://github.com/otmon76/cs.javascript.info into otmon76-1.14.3
2 parents 1fce745 + 7387d17 commit 8dc26c7

File tree

2 files changed

+76
-100
lines changed
  • 1-js
    • 06-advanced-functions/09-call-apply-decorators/03-debounce/debounce.view
    • 99-js-misc/03-currying-partials

2 files changed

+76
-100
lines changed

1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/debounce.view/index.html

Lines changed: 0 additions & 24 deletions
This file was deleted.

1-js/99-js-misc/03-currying-partials/article.md

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ libs:
33

44
---
55

6-
# Currying
6+
# Curryování
77

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.
99

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)`.
1111

12-
Currying doesn't call a function. It just transforms it.
12+
Curryování nevolá funkci, jen ji transformuje.
1313

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.
1515

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)`:
1717

1818
```js run
1919
*!*
20-
function curry(f) { // curry(f) does the currying transform
20+
function curry(f) { // curry(f) provede curryovací transformaci
2121
return function(a) {
2222
return function(b) {
2323
return f(a, b);
@@ -26,163 +26,163 @@ function curry(f) { // curry(f) does the currying transform
2626
}
2727
*/!*
2828

29-
// usage
30-
function sum(a, b) {
29+
// použití
30+
function součet(a, b) {
3131
return a + b;
3232
}
3333

34-
let curriedSum = curry(sum);
34+
let curryovanýSoučet = curry(součet);
3535

36-
alert( curriedSum(1)(2) ); // 3
36+
alert( curryovanýSoučet(1)(2) ); // 3
3737
```
3838

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.
4040

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`.
4444

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ě:
4646

4747
```js run
48-
function sum(a, b) {
48+
function součet(a, b) {
4949
return a + b;
5050
}
5151

52-
let curriedSum = _.curry(sum); // using _.curry from lodash library
52+
let curryovanýSoučet = _.curry(součet); // použijeme _.curry z knihovny lodash
5353

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ě
55+
alert( curryovanýSoučet(1)(2) ); // 3, voláno parciálně
5656
```
5757

58-
## Currying? What for?
58+
## Curryování? K čemu to je?
5959

60-
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.
6161

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`:
6363

6464
```js
65-
function log(date, importance, message) {
66-
alert(`[${date.getHours()}:${date.getMinutes()}] [${importance}] ${message}`);
65+
function log(datum, důležitost, zpráva) {
66+
alert(`[${datum.getHours()}:${datum.getMinutes()}] [${důležitost}] ${zpráva}`);
6767
}
6868
```
6969

70-
Let's curry it!
70+
Zcurryujme ji!
7171

7272
```js
7373
log = _.curry(log);
7474
```
7575

76-
After that `log` works normally:
76+
Pak bude `log` fungovat normálně:
7777

7878
```js
79-
log(new Date(), "DEBUG", "some debug"); // log(a, b, c)
79+
log(new Date(), "LADĚNÍ", "nějaké ladění"); // log(a, b, c)
8080
```
8181

82-
...But also works in the curried form:
82+
...Ale bude fungovat i v curryované formě:
8383

8484
```js
85-
log(new Date())("DEBUG")("some debug"); // log(a)(b)(c)
85+
log(new Date())("LADĚNÍ")("nějaké ladění"); // log(a)(b)(c)
8686
```
8787

88-
Now we can easily make a convenience function for current logs:
88+
Nyní můžeme snadno vytvořit pohodlnou funkci pro aktuální logování:
8989

9090
```js
91-
// logNow will be the partial of log with fixed first argument
92-
let logNow = log(new Date());
91+
// logNyní bude parciální log s pevným prvním argumentem
92+
let logNyní = log(new Date());
9393

94-
// use it
95-
logNow("INFO", "message"); // [HH:mm] INFO message
94+
// použijeme ji
95+
logNyní("INFO", "zpráva"); // [HH:mm] INFO zpráva
9696
```
9797

98-
Now `logNow` is `log` with fixed first argument, in other words "partially applied function" or "partial" for short.
98+
Nyní `logNyní` je `log` s pevným prvním argumentem, jinými slovy „parciálně aplikovaná funkce“ nebo krátce „parciální funkce“.
9999

100-
We can go further and make a convenience function for current debug logs:
100+
Můžeme jít dál a vytvořit pohodlnou funkci pro aktuální logování ladění:
101101

102102
```js
103-
let debugNow = logNow("DEBUG");
103+
let laděníNyní = logNyní("LADĚNÍ");
104104

105-
debugNow("message"); // [HH:mm] DEBUG message
105+
laděníNyní("zpráva"); // [HH:mm] LADĚNÍ zpráva
106106
```
107107

108-
So:
109-
1. We didn't lose anything after currying: `log` is still callable normally.
110-
2. We can easily generate partial functions such as for today's logs.
108+
Tedy:
109+
1. Po curryování nic neztratíme: `log` se stále dá volat běžným způsobem.
110+
2. Můžeme snadno generovat parciální funkce, např. pro logování s dnešním datem.
111111

112-
## Advanced curry implementation
112+
## Pokročilá implementace curryování
113113

114-
In case you'd like to get in to the details, here's the "advanced" curry implementation for multi-argument functions that we could use above.
114+
Pro případ, že byste chtěli zajít do detailů, je zde „pokročilá“ implementace curryování pro víceargumentové funkce, kterou bychom mohli použít výše.
115115

116-
It's pretty short:
116+
Je opravdu krátká:
117117

118118
```js
119-
function curry(func) {
119+
function curry(funkce) {
120120

121-
return function curried(...args) {
122-
if (args.length >= func.length) {
123-
return func.apply(this, args);
121+
return function curryovaná(...args) {
122+
if (args.length >= funkce.length) {
123+
return funkce.apply(this, args);
124124
} else {
125125
return function(...args2) {
126-
return curried.apply(this, args.concat(args2));
126+
return curryovaná.apply(this, args.concat(args2));
127127
}
128128
}
129129
};
130130

131131
}
132132
```
133133

134-
Usage examples:
134+
Příklady použití:
135135

136136
```js
137-
function sum(a, b, c) {
137+
function součet(a, b, c) {
138138
return a + b + c;
139139
}
140140

141-
let curriedSum = curry(sum);
141+
let curryovanýSoučet = curry(součet);
142142

143-
alert( curriedSum(1, 2, 3) ); // 6, still callable normally
144-
alert( curriedSum(1)(2,3) ); // 6, currying of 1st arg
145-
alert( curriedSum(1)(2)(3) ); // 6, full currying
143+
alert( curryovanýSoučet(1, 2, 3) ); // 6, stále normálně volatelná
144+
alert( curryovanýSoučet(1)(2,3) ); // 6, curryování 1. argumentu
145+
alert( curryovanýSoučet(1)(2)(3) ); // 6, úplné curryování
146146
```
147147

148-
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.
149149

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:
151151

152152
```js
153-
// func is the function to transform
154-
function curried(...args) {
155-
if (args.length >= func.length) { // (1)
156-
return func.apply(this, args);
153+
// funkce je funkce, která se má transformovat
154+
function curryovaná(...args) {
155+
if (args.length >= funkce.length) { // (1)
156+
return funkce.apply(this, args);
157157
} else {
158158
return function(...args2) { // (2)
159-
return curried.apply(this, args.concat(args2));
159+
return curryovaná.apply(this, args.concat(args2));
160160
}
161161
}
162162
};
163163
```
164164

165-
When we run it, there are two `if` execution branches:
165+
Když ji spustíme, obsahuje dvě běhové větve `if`:
166166

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.
169169

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.
171171

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ů.
174174
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.
176176
```
177177

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)`.
180180
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ě.
182182
```
183183

184-
## Summary
184+
## Shrnutí
185185

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.
187187

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

Comments
 (0)