Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78f1f17

Browse files
authored
Currying
1 parent 620b8b7 commit 78f1f17

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,23 @@ console.log(twoByTwo); // [[a, b], [b, c], [c, d], [d, e], [e, f], [f, g]]
9393
```
9494

9595
<a href="https://codepen.io/Bunlong/pen/JBJWRa" target="_blank">Edit on Codepen</a>
96+
97+
## Currying
98+
99+
Currying allows a function with multiple arguments to be translated into a sequence of functions. Curried functions can be tailored to match the signature of another function.
100+
101+
```javascript
102+
const convertUnits = (toUnit, factor, offset = 0) => input => ((offset + input) * factor).toFixed(2).concat(toUnit);
103+
const milesToKm = convertUnits('km', 1.60936, 0);
104+
const poundsToKg = convertUnits('kg', 0.45460, 0);
105+
const farenheitToCelsius = convertUnits('degrees C', 0.5556, -32);
106+
milesToKm(10); // "16.09 km"
107+
poundsToKg(2.5); // "1.14 kg"
108+
farenheitToCelsius(98); // "36.67 degrees C"
109+
110+
const weightsInPounds = [5, 15.4, 9.8, 110];
111+
// without currying
112+
// const weightsInKg = weightsInPounds.map(x => convertUnits('kg', 0.45460, 0)(x));
113+
// with currying
114+
const weightsInKg = weightsInPounds.map(poundsToKg); // 2.27kg, 7.00kg, 4.46kg, 50.01kg
115+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /