Last active
November 11, 2015 19:07
-
-
Save spacious/2880283c6110a6cf98ed to your computer and use it in GitHub Desktop.
basic lift
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
// code incomplete, example only | |
Just.prototype.ap = function(m) { | |
return m.map(this.value); | |
}; | |
Nothing.prototype.ap = () => { return this } | |
function lift2(f, a1, a2){ | |
return a1.map(f).ap(a2) | |
} | |
// Maybe -> Number | |
const a1 = Maybe(2) // Just { value: 2 } | |
// T -> U -> T | |
const f = x => fn => fn(x) | |
// Number -> Number | |
const sqr = y => y * y | |
// Maybe -> T -> T -> Maybe -> T | |
const liftN = lift2(f, a1) | |
// Maybe -> Number -> Number | |
const boxSquare = Maybe(sqr) | |
// Maybe -> Number | |
const squared = liftN(boxSquare) // Just { value: 4 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment