I'm considering how best to curry functions in JS (w/out an external library).
I can see two ways of doing so, and am interested in what others see as the advantages/disadvantages of each approach:
Write functions that only take a single argument and return other functions:
const add = (a) => (b) => a + b;
// Currying is trivial
const add1 = add(1)
// Using w/out currying is awkward:
add(1)(2) // => 3
A downside is that this can lead to more cluttered syntax for more complex functions:
const add = (a) {
return (b) => {
return a + b
}
}
Write functions as you would if currying wasn't a concern:
const add = (a, b) => a + b;
Curry by wrapping the function with a new function:
const add1 = (b) => add(1, b)
On the one hand, the implementation of the original function is less surprising. OTOH, it's more effort to create the curried function.
Is one obviously better? IDK. I slightly prefer the 2nd option; it seems more concrete to understand. I like that the curried function is something I can create if I want, rather than something I have to use.
I'm interested in other's opinions.