Skip to content

Instantly share code, notes, and snippets.

@brauliodiez
Created June 4, 2017 09:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brauliodiez/1e358bd8745f846d1e11518b5abc4e73 to your computer and use it in GitHub Desktop.
Save brauliodiez/1e358bd8745f846d1e11518b5abc4e73 to your computer and use it in GitHub Desktop.
javascript currying

Currying

Currying is the act of turning a function into a new function that takes slightly fewer arguments, achieving a slightly more specific task.

http://macr.ae/article/es6-and-currying.html

Steps

Let's start by making a simple function that will sum up two numbers, using currying:

Let's build the sample using normal functions

function sum(a) {
  return function second(b) {
    return a + b;
  }
}

console.log(sum(3)(4));

Let's build the same using fat arrow:

const sum = a => b => {
  return a + b;
}

console.log(sum(3)(4));

We could use currying when e.g. we need to inform about a given parameter on a standard event class.

Pseudocode:

onClick (color) => (e) {
   props.setColor(color);
}


<input 
  onclick="onClick('red')"
/>

<input
  onclick="onClick('green')"
/>


<input
  onclick="onClick('blue')"
/>

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