Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotlolita/a6c439dbb9afd5f1990e to your computer and use it in GitHub Desktop.
Save robotlolita/a6c439dbb9afd5f1990e to your computer and use it in GitHub Desktop.
// :: 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