Skip to content

Instantly share code, notes, and snippets.

@leroix
Created May 26, 2015 01:52
Show Gist options
  • Save leroix/3e9d0443864adf1974ca to your computer and use it in GitHub Desktop.
Save leroix/3e9d0443864adf1974ca to your computer and use it in GitHub Desktop.
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