Generic currying

This commit is contained in:
Angelos Chalaris
2017-12-10 15:21:35 +02:00
parent a0b7068cc1
commit 367ee7f390
2 changed files with 24 additions and 0 deletions

View File

@ -13,6 +13,7 @@
* [Capitalize first letter](#capitalize-first-letter) * [Capitalize first letter](#capitalize-first-letter)
* [Count occurences of a value in array](#count-occurences-of-a-value-in-array) * [Count occurences of a value in array](#count-occurences-of-a-value-in-array)
* [Current URL](#current-url) * [Current URL](#current-url)
* [Curry](#curry)
* [Difference between arrays](#difference-between-arrays) * [Difference between arrays](#difference-between-arrays)
* [Distance between two points](#distance-between-two-points) * [Distance between two points](#distance-between-two-points)
* [Even or odd number](#even-or-odd-number) * [Even or odd number](#even-or-odd-number)
@ -89,6 +90,18 @@ Use `window.location.href` to get current URL.
var currentUrl = _ => window.location.href; var currentUrl = _ => window.location.href;
``` ```
### Curry
Use recursion.
If the number of provided arguments (`args`) is sufficient, call the passed function `f`.
Otherwise return a curried function `f` that expects the rest of the arguments.
```js
curry = f =>
(...args) =>
args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs)
```
### Difference between arrays ### Difference between arrays
Use `filter()` to remove values that are part of `values`, determined using `indexOf()`. Use `filter()` to remove values that are part of `values`, determined using `indexOf()`.

11
snippets/curry.md Normal file
View File

@ -0,0 +1,11 @@
### Curry
Use recursion.
If the number of provided arguments (`args`) is sufficient, call the passed function `f`.
Otherwise return a curried function `f` that expects the rest of the arguments.
```js
curry = f =>
(...args) =>
args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs)
```