Skip to content

Instantly share code, notes, and snippets.

@choonkending
Created June 1, 2017 21:17
Show Gist options
  • Save choonkending/500db30da1c0cb8838dcfc65ea56862c to your computer and use it in GitHub Desktop.
Save choonkending/500db30da1c0cb8838dcfc65ea56862c to your computer and use it in GitHub Desktop.
/* 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