Last active
August 21, 2019 04:20
-
-
Save gabefinch/5525011c92661bf74a1b416c573afefa 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
// Identity functor | |
const Box = x => ({ | |
map: f => Box(f(x)), | |
fold: f => f(x), | |
inspect: () => `Box(${x})` | |
}); | |
// const Either = Right || Left | |
const Right = x => ({ | |
chain: f => f(x), | |
map: f => Right(f(x)), | |
fold: (f, g) => g(x), | |
inspect: () => `Right(${x})` | |
}); | |
const Left = x => ({ | |
chain: f => Left(x), | |
map: f => Left(x), | |
fold: (f, g) => f(x), | |
inspect: () => `Left(${x})` | |
}); | |
const fromNullable = x => x != null ? Right(x) : Left(x)); | |
const tryCatch = f => { | |
try { | |
return Right(f()); | |
} | |
catch(e) { | |
return Left(e); | |
} | |
} | |
// Semigroups | |
const All = x => ({ | |
x, | |
concat: ({ x: y }) => All(x && y), | |
inspect: () => `All(${x})`; | |
}); | |
const First = x => ({ | |
x, | |
concat: _ => First(x), | |
inspect: () => `First(${x})` | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment