Skip to content

Instantly share code, notes, and snippets.

@spacious
Last active November 11, 2015 19:07
Show Gist options
  • Save spacious/2880283c6110a6cf98ed to your computer and use it in GitHub Desktop.
Save spacious/2880283c6110a6cf98ed to your computer and use it in GitHub Desktop.
basic lift
// 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