import { default } from "https://deno.land/x/functional@v1.3.4/mod.js";
Either
The Either
is a sum type similar to Maybe
, but it differs in that a value can be of two possible types
(Left or Right). Commonly the Left type represents an error.
The Either
type implements the following algebras:
- [x] Alternative
- [x] Comonad
- [x] Monad
Example
import Either from "https://deno.land/x/functional@v1.3.2/library/Either.js";
const containerA = Either.Right(42).map(x => x + 2);
const containerB = Either.Left(new Error("The value is not 42.")).map(x => x + 2);
const containerC = containerB.alt(containerA);
assert(Either.Right.is(containerA));
assert(containerA.extract() === 44);
assert(Either.Left.is(containerB));
assert(Either.Right(containerC));