News for hackers. Stuff that matters.
Re:Currying in JavaScript by ufo on Sunday July 19, 2015 @08:14PM (#9913388)
I didn't have a great experience with currying in Javascript when I tried to use it. In Haskell passing the wrong number of arguments to a function results in an immediate type error even when currying while in Javascript the error is going to end up being shallowed by the currying. You will only notice the problem a lot later, when you notice that something is a partially evaluated function when it shouldn't.

Another problem with currying in Javascript is that it leads to more complicated stack traces, which is unfortunate for debugging.

Re:Currying in JavaScript by jfarmer on Monday July 20, 2015 @12:31AM (#9914000)
I might be misunderstanding your point about arity-related errors, but every function in JavaScript is variadic, i.e., you can pass any number of arguments to any function and JavaScript will happily call it. Excess arguments vanish into the aether and un-supplied arguments arrive with a value of "undefined". So, to the extent that what you describe is a problem, it's a problem baked into the foundation of the language. :D

The OP is already using Function.length (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...), so I suppose one could roll their own arity-checking. It'd be a little out of sync with the rest of the language, though.

Re:Currying in JavaScript by dllthomas on Monday July 20, 2015 @01:50AM (#9914206)
While I've not messed with currying in JS, I can see a difference still.

If you have an uncurried function for which you have not supplied sufficient arguments, and it is eagerly evaluated, you more likely see below the call site an error in the function where an expected value is undefined. If the function is curried, the meat of the function remains unevaluated, and so is sure to give you back a value that isn't the one you want. So it does seem like moving to currying in JS is likely to sometimes increase error-to-symptom distance.

Re:Currying in JavaScript by antimagic on Monday July 20, 2015 @05:37AM (#9914896)
Obviously the solution as proposed only allows for non-variadic functions to be curried (where 'non-variadic' means that the function needs to be called with a fixed number of arguments to work correctly, not that it can't be called with the wrong number of arguments, because clearly Javascript doesn't care).

Does currying even make sense for variadic functions? I can't imagine what that would look like as an implementation.

Re:Currying in JavaScript by ufo on Monday July 20, 2015 @09:40AM (#9915935)
Its possible to curry a variadic function if there is a way to know when you reached the last argument. For example, in a printf-like function you know the number of inputs once you parse the format string.

This example here is a function that sums a variadic list of non-zero numbers. The zero signals the end of the list.

http://pastebin.com/xQRbRcJe

Its certainly very weird though. I wouldn't recommend doing this in real programs.

Re:Currying in JavaScript by zeckalpha on Sunday July 19, 2015 @09:06PM (#9913526)
Perhaps this method could be used by a strongly-typed compile-to-js curried language. (PureScript, Elm, and friends)
Re:Currying in JavaScript by skwosh on Sunday July 19, 2015 @09:23PM (#9913569)
I haven't used either but since they're based on Haskell I would assume that currying is inherent to the language (functions are curried by default).
Re:Currying in JavaScript by zeckalpha on Sunday July 19, 2015 @09:35PM (#9913604)
Hence a "curried language".
Re:Currying in JavaScript by tel on Sunday July 19, 2015 @10:02PM (#9913690)
Yeah, but a technique like this just isn't useful there. The host language handles when function application occurs.
Re:Currying in JavaScript by zeckalpha on Friday July 24, 2015 @09:19AM (#9941743)
No, curried languages compiled output typically has tricks like in the OP and they typically rely on a run time system to help with the 'impedance mismatch' between curried and not languages.