Created
October 25, 2015 19:55
-
-
Save ivancorrales/9123a657bf743bd6b22a to your computer and use it in GitHub Desktop.
Curried javascript functions example
This file contains 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
#!/usr/bin/env node | |
function add(param1){ | |
return function(param2){ | |
return param1+param2; | |
} | |
} | |
function substract(param1){ | |
return function(param2){ | |
return param1-param2; | |
} | |
} | |
function swap(fn){ | |
return function(b,c){ | |
return fn(c)(b); | |
} | |
} | |
function evaluate(operation){ | |
return function(param1){ | |
var myOperation = operation(param1); | |
return function(param2){ | |
return myOperation(param2); | |
} | |
} | |
} | |
var value = swap(substract) (evaluate(substract)(evaluate(add)(8)(1))(2),evaluate(substract)(evaluate(add)(6)(2))(2)); | |
console.log('( ( 6 + 2 ) - 2 ) - ( ( 8 + 1 ) - 2 ) = '+value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment