From 367ee7f390beaf2fcff007dc9378adf34c32cae5 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 10 Dec 2017 15:21:35 +0200 Subject: [PATCH] Generic currying --- README.md | 13 +++++++++++++ snippets/curry.md | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 snippets/curry.md diff --git a/README.md b/README.md index daf8d81dd..0a64be235 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ * [Capitalize first letter](#capitalize-first-letter) * [Count occurences of a value in array](#count-occurences-of-a-value-in-array) * [Current URL](#current-url) +* [Curry](#curry) * [Difference between arrays](#difference-between-arrays) * [Distance between two points](#distance-between-two-points) * [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; ``` +### 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 Use `filter()` to remove values that are part of `values`, determined using `indexOf()`. diff --git a/snippets/curry.md b/snippets/curry.md new file mode 100644 index 000000000..9a090fe53 --- /dev/null +++ b/snippets/curry.md @@ -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) +```