Skip to content

Instantly share code, notes, and snippets.

@ktheory
Last active September 22, 2016 17:43
Show Gist options
  • Save ktheory/fb11fc5524537da63948ad72239a739e to your computer and use it in GitHub Desktop.
Save ktheory/fb11fc5524537da63948ad72239a739e to your computer and use it in GitHub Desktop.
A tale of two curries

A tale of two curries

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:

Option 1: curry-first design

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
  }
}

Option 2: Curry by wrapping

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.

Conclusion:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment