Created
June 1, 2017 21:17
-
-
Save choonkending/500db30da1c0cb8838dcfc65ea56862c 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
/* Option types */ | |
const Option = x => (x === undefined || x === null) ? None : Some(x); | |
const Some = x => ({ | |
filter: f => f(x) ? Some(x) : None, | |
map: f => Some(f(x)), | |
fold: (ifEmpty, f) => f(x), | |
getOrElse: defaultValue => x | |
}); | |
const None = { | |
filter: f => None, | |
map: f => None, | |
fold: (ifEmpty, f) => ifEmpty(), | |
getOrElse: defaultValue => defaultValue | |
}; | |
/* Either Types */ | |
const Left = x => ({ | |
map: f => Left(x), | |
fold: (ifLeft, ifRight) => ifLeft(x) | |
}); | |
const Right = x => ({ | |
map: f => Right(f(x)), | |
fold: (ifLeft, ifRight) => ifRight(x) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment