Last active
January 7, 2018 23:24
-
-
Save Gaafar/c108157559703a277c7d to your computer and use it in GitHub Desktop.
ES6 Currying Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Yeah, this isn't how curry works.
<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
This seems like partial application not curry.
With curry should you be able to do this:
var x = (a, b, c, d) => a + b + c + d; var y = curry(x)(1)(2)(3)(4);