Created
May 26, 2015 01:52
-
-
Save leroix/3e9d0443864adf1974ca to your computer and use it in GitHub Desktop.
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
var combinators = require('fantasy-combinators') | |
var compose = combinators.compose | |
var identity = combinators.identity | |
var flip = combinators.flip | |
var add = function (a) { | |
return function (b) { | |
return b + a | |
} | |
} | |
var mul = function (a) { | |
return function (b) { | |
return b * a | |
} | |
} | |
var uncurry2 = function (f) { | |
return function (a, b) { | |
return f(a)(b) | |
} | |
} | |
var reduce = function (reducer) { | |
return function (xs) { | |
return function (initial_val) { | |
return xs.reduce(uncurry2(reducer), initial_val) | |
} | |
} | |
} | |
var compose_n = flip (reduce (compose)) (identity) | |
// f(x) = 2(5x + 3) = mul(2)(add(3)(mul(5)(x))) | |
var derp = compose_n ([mul(2), add(3), mul(5)]) | |
// 56 | |
console.log(derp(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment