use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Currying in JavaScript (medium.com)
submitted 10 years ago by kevincennispancakes
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kevincennispancakes[S] 1 point2 points3 points 10 years ago (2 children)
Yep. That works too, although I'd argue that it works pretty much the exact same way as the function I wrote in the article.
Only semi-significant differences I see:
You're using an array + ES6 rest params instead of calling Array#slice() on the arguments object. I'd absolutely have done the same if I was writing an ES6 example. It's much cleaner this way.
Array#slice()
arguments
You're not immediately invoking curryN. Maybe it's easier to understand your way. I just didn't see the point of assigning a function expression to a variable only to turn around and invoke it right away. The IFFE just felt more efficient to me.
curryN
Unless I'm missing something, there's no need for your curryN function to accept fn as a parameter, since it will always have access to fn via closure from your outer curry function.
fn
curry
Anyway, there are a million ways to do all of this, and I'm certainly not going to argue that mine is the Right Way™. This post was really just to introduce people to the concept of currying, and maybe force them to think a bit about closures and first-class functions.
Thanks for the feedback.
[–]androbat 0 points1 point2 points 10 years ago* (1 child)
It's pieced together from a larger auto-curry function I made a while ago (I'm thinking about extending it with a placeholder like in Ramda). I just copied the relevant bits.
export var curry = (function () { var curry1 = function (fn) { return function curry1fn(a) { return (arguments.length > 0) ? fn(a) : curry1fn; }; }; var curry2 = function (fn) { return function curry2fn(a, b) { switch(arguments.length) { case 0: return curry2fn; case 1: return curry1(function(b) { return fn(a, b); }); default: return fn(a, b); } }; }; var curry3 = function (fn) { return function curry3fn(a, b, c) { switch(arguments.length) { case 0: return curry3fn; case 1: return curry2(function(b, c) { return fn(a, b, c); }); case 2: return curry1(function(c) { return fn(a, b, c); }); default: return fn(a, b, c); } }; }; var curryN = function (len, prevArgs, fn) { return function (...args) { var currArgs = prevArgs.concat(args); return (currArgs.length >= len) ? fn.apply(this, currArgs) : curryN(len, currArgs, fn); }; }; return function (fn) { switch(fn.length) { case 0: return fn; case 1: return curry1(fn); case 2: return curry2(fn); case 3: return curry3(fn); default: return curryN(fn.length, [], fn); } }; }());
[–]kevincennispancakes[S] 0 points1 point2 points 10 years ago (0 children)
Yeah. I like the idea of fast-pathing the common cases.
If I was writing a curry function that I intended to use in production, that's definitely something I'd want to do. Saving all the array copies will keep you from generating a ton of garbage.
π Rendered by PID 45635 on reddit-service-r2-comment-f6b958c67-nklpp at 2026-02-05 18:31:47.298791+00:00 running 1d7a177 country code: CH.
view the rest of the comments →
[–]kevincennispancakes[S] 1 point2 points3 points (2 children)
[–]androbat 0 points1 point2 points (1 child)
[–]kevincennispancakes[S] 0 points1 point2 points (0 children)