loading...

Discussion on: What the heck is Currying anyway?

heytulsiprasad profile image
Tulsi Prasad Author

I did it to show that we can also use curryMultiply in the way we normally should have used multiply function. Also, I felt it was kinda obvious that multiply(2, 4) will always return 8 as its just a normal function.

Thread Thread
mikeschinkel profile image
Mike Schinkel

You can call curryMultiply() with two parameters and it will pass them all? I thought it only worked with one parameter (I have a lot more experience with PHP and Go than with Javascript, so I do not know how JS works well enough.)

If it can be used that way then can you please explain that in comments in the code snippet or elsewhere in the article to clarify for others who assume the same as me because that was not at all clear to me when reading?

Thread Thread
heytulsiprasad profile image
Tulsi Prasad Author

Oops I got caught up in college stuff, apologies!

For now you can fiddle around with this repl to understand how it works! If possible, I'll add a line about this on the blog tomorrow. 🤞 Link to repl

The actual thing is as these two lines of code exist in our custom curry function:

  if (args.length >= func.length) {
      return func.apply(undefined, args);

We check if we already have got enough arguments that are necessary for the function to execute (or arity of that function), as in this case curryMultiply(2, 4). If this condition holds true, we apply (or invoke) the function right away instead of awaiting for another argument like we do for cases like curryMultiply(2)(4)

which is when the else block of the code executes

} else {
      return function (...rest) {
        return curried.apply(undefined, rest.concat(args));
      };
Thread Thread
mikeschinkel profile image
Mike Schinkel

Thanks for the detailed reply and REPL.

P.S. RE: "College stuff"; I don't envy you! I did my time years ago and thinking about it still gives me PTSD! :-D