Last active
August 29, 2015 14:07
-
-
Save robotlolita/a6c439dbb9afd5f1990e to your computer and use it in GitHub Desktop.
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
// :: String -> String | |
function concat(a){ return function(b){ | |
return a + b | |
}} | |
// :: (a -> b -> c) -> (b -> a -> c) | |
function flip(f){ return function(a){ return function(b) { | |
return f(b)(a) | |
}}} | |
// :: [[a]] -> [a] | |
function flatten(xss) { | |
return xss.reduce(function(ys, xs) { | |
return ys.concat(xs) | |
}, []) | |
} | |
// :: [a] -> (a -> [b]) -> [b] | |
function chain(xs){ return function(f) { | |
return flatten(xs.map(f)) | |
}} | |
// :: [(a -> b)] -> [a] -> [b] | |
function ap(fs){ return function(b) { | |
return chain(b)(function(x) { | |
return fs.map(function(f) { | |
return f(x) | |
}) | |
}) | |
}} | |
var xs = [['hello', 'hi'], ['there', 'stranger']] | |
var greetings = xs[0].map(flip(concat(' ')) // adds a whitespace after every greeting | |
var what = xs[1] | |
ap(greetings.map(concat)) // transforms each greeting in a function String -> String | |
(what) // distributes those functions over the list of things |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment