Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Last active Aug 29, 2015
Embed
What would you like to do?
// :: 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