Skip to content

Instantly share code, notes, and snippets.

@Gaafar
Last active January 7, 2018 23:24
Show Gist options
  • Save Gaafar/c108157559703a277c7d to your computer and use it in GitHub Desktop.
Save Gaafar/c108157559703a277c7d to your computer and use it in GitHub Desktop.
ES6 Currying Example
function add (x, y) {return x+y}
let curry = (f, ...args) => f.bind.apply(f,[null, ...args])
let add4 = curry(add,4)
console.log(add4(5)) //9
let add4n6 = curry(add4,6)
console.log(add4n6()) //10
// try in babel
// https://babeljs.io/repl/#?evaluate=true&presets=es2015&experimental=false&loose=false&spec=false&code=function%20add%20(x%2C%20y)%20%7Breturn%20x%2By%7D%0A%0Alet%20curry%20%3D%20(f%2C%20...args)%20%3D%3E%20f.bind.apply(f%2C%5Bnull%2C%20...args%5D)%0A%0Alet%20add4%20%3D%20curry(add%2C4)%0Aconsole.log(add4(5))%20%2F%2F9%0A%0Alet%20add4n6%20%3D%20curry(add4%2C6)%0Aconsole.log(add4n6())%20%2F%2F10
@jcready
Copy link

jcready commented Mar 10, 2016

Yeah, this isn't how curry works.

@diasoluyalu
Copy link

diasoluyalu commented Jan 7, 2018

<script type="text/javascript"> "use strict"; var x = (a) => (b) => (c) => (d) => a + b + c + d; console.log( x(1)(2)(3)(4) ); </script>

At the console :

10

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