From 101bcf6d8b96863d2366113517f5bffabb67140e Mon Sep 17 00:00:00 2001 From: Elder Henrique Souza Date: Tue, 12 Dec 2017 20:26:01 -0200 Subject: [PATCH 1/2] refactor to account for variadic functions altough certainly more verbose, this version accounts for variadic functions such as Math.min that can't represent it's arity with the length property properly. for such cases you can pass the optional argument arity (as in the edited example). what do you think? --- snippets/curry.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/snippets/curry.md b/snippets/curry.md index a700ac776..78cf79702 100644 --- a/snippets/curry.md +++ b/snippets/curry.md @@ -5,8 +5,15 @@ If the number of provided arguments (`args`) is sufficient, call the passed func Otherwise return a curried function `f` that expects the rest of the arguments. ```js -const curry = f => - (...args) => - args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs); +const curry = (f, arity = f.length, next) => + (next = prevArgs => + nextArg => { + const args = [ ...prevArgs, nextArg ] + return args.length >= arity + ? f(...args) + : next(args); + } + )([]); // curry(Math.pow)(2)(10) -> 1024 +// curry(Math.min, 3)(10)(50)(2) -> 2 ``` From e91fc01d2507f885ea43d1497bf1decaae103269 Mon Sep 17 00:00:00 2001 From: Elder Henrique Souza Date: Wed, 13 Dec 2017 09:31:25 -0200 Subject: [PATCH 2/2] Update curry.md --- snippets/curry.md | 1 + 1 file changed, 1 insertion(+) diff --git a/snippets/curry.md b/snippets/curry.md index 78cf79702..4bcfee3c6 100644 --- a/snippets/curry.md +++ b/snippets/curry.md @@ -3,6 +3,7 @@ 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. +If you want to curry a function that accepts a variable number of arguments (variadic function) like Math.min for example, you can optionally pass the number of arguments to the second parameter arity. ```js const curry = (f, arity = f.length, next) =>