Returns true if this is a Right and its value is equal to elem
(as determined by the equals protocol), returns false otherwise.
// True
Right("something").contains("something")
// False because the values are different
Right("something").contains("anything") // false
// False because the source is a `left`
Left("something").contains("something") // false
Implements IEquals.equals.
is the right hand side of the equality check
Returns false if the source is a left, or returns the result
of the application of the given predicate to the right value.
// True, because it is a right and predicate holds
Right(20).exists(n => n > 10)
// False, because the predicate returns false
Right(10).exists(n => n % 2 != 0)
// False, because it is a left
Left(10).exists(n => n == 10)
Filters right values with the given predicate, returning
the value generated by zero in case the source is a right
value and the predicate doesn't hold.
Possible outcomes:
right if this is a right value and the
given predicate p holds for itLeft(zero()) if this is a right value
and the given predicate p does not holdLeftRight(12).filterOrElse(x => x > 10, () => -1) // Right(12)
Right(7).filterOrElse(x => x > 10, () => -1) // Left(-1)
Left(7).filterOrElse(x => false, () => -1) // Left(7)
Applies the left function to Left values, and the
right function to Right values and returns the result.
const maybeNum: Either<string, number> =
tryParseInt("not a number")
const result: string =
maybeNum.fold(
str => `Could not parse string: ${str}`,
num => `Success: ${num}`
)
Returns true if the source is a left or returns
the result of the application of the given predicate to the
right value.
// True, because it is a `left`
Left("hello").forAll(x => x > 10)
// True, because the predicate holds
Right(20).forAll(x => x > 10)
// False, it's a right and the predicate doesn't hold
Right(7).forAll(x => x > 10)
Executes the given side-effecting function if the
source is a right value.
Right(12).forAll(console.log) // prints 12
Left(10).forAll(console.log) // silent
Returns the Right value, if the source has one,
otherwise throws an exception.
WARNING!
This function is partial, the Either must be a Right, otherwise
a runtime exception will get thrown. Use with care.
Returns the value from this right or the given fallback
value if this is a left.
Right(10).getOrElse(27) // 10
Left(10).getOrElse(27) // 27
Returns the value from this right or a value generated
by the given thunk if this is a left.
Right(10).getOrElseL(() => 27) // 10
Left(10).getOrElseL(() => 27) // 27
Implements IEquals.hashCode.
Returns true if this is a left, false otherwise.
Left("hello").isLeft() // true
Right(10).isLeft() // false
Returns true if this is a right, false otherwise.
Left("hello").isRight() // false
Right(10).isRight() // true
Transform the source if it is a right with the given
mapping function.
Right(10).map(x => x + 17) // right(27)
Left(10).map(x => x + 17) // left(10)
If this is a left, then return the left value as a right
or vice versa.
Right(10).swap() // left(10)
Left(20).swap() // right(20)
Returns an Option.some(right) if the source is a right value,
or Option.none in case the source is a left value.
Maps 2 Either values by the mapping function, returning a new
Either reference that is a Right only if both Either values are
Right values, otherwise it returns the first Left value noticed.
// Yields Right(3)
Try.map2(Right(1), Right(2),
(a, b) => a + b
)
// Yields Left, because the second arg is a Left
Try.map2(Right(1), Left("error"),
(a, b) => a + b
)
This operation is the Applicative.map2.
Maps 3 Either values by the mapping function, returning a new
Either reference that is a Right only if all 3 Either values are
Right values, otherwise it returns the first Left value noticed.
// Yields Right(6)
Try.map3(Right(1), Right(2), Right(3),
(a, b, c) => a + b + c
)
// Yields Left, because the second arg is a Left
Try.map3(Right(1), Left("error"), Right(3),
(a, b, c) => a + b + c
)
Maps 4 Either values by the mapping function, returning a new
Either reference that is a Right only if all 4 Either values are
Right values, otherwise it returns the first Left value noticed.
// Yields Right(10)
Try.map4(Right(1), Right(2), Right(3), Right(4),
(a, b, c, d) => a + b + c + d
)
// Yields Left, because the second arg is a Left
Try.map4(Right(1), Left("error"), Right(3), Right(4),
(a, b, c, d) => a + b + c + d
)
Maps 5 Either values by the mapping function, returning a new
Either reference that is a Right only if all 5 Either values are
Right values, otherwise it returns the first Left value noticed.
// Yields Right(15)
Try.map5(Right(1), Right(2), Right(3), Right(4), Right(5),
(a, b, c, d, e) => a + b + c + d + e
)
// Yields Left, because the second arg is a Left
Try.map5(Right(1), Left("error"), Right(3), Right(4), Right(5),
(a, b, c, d, e) => a + b + c + d + e
)
Maps 6 Either values by the mapping function, returning a new
Either reference that is a Right only if all 6 Either values are
Right values, otherwise it returns the first Left value noticed.
// Yields Right(21)
Try.map5(Right(1), Right(2), Right(3), Right(4), Right(5), Right(6),
(a, b, c, d, e, f) => a + b + c + d + e + f
)
// Yields Left, because the second arg is a Left
Try.map5(Right(1), Left("error"), Right(3), Right(4), Right(5), Right(6),
(a, b, c, d, e, f) => a + b + c + d + e + f
)
Builds a pure Either value.
This operation is the pure Applicative operation for lifting
a value in the Either context.
Keeps calling f until a Right(b) is returned.
Based on Phil Freeman's Stack Safety for Free.
Described in FlatMap.tailRecM.
Generated using TypeDoc
Represents a value of one of two possible types (a disjoint union).
A common use of Either is as an alternative to Option for dealing with possible missing values. In this usage Option.none is replaced with Either.left which can contain useful information and Option.some is replaced with Either.right.
Convention dictates that
leftis used for failure andrightis used for success. Note that thisEithertype is right-biased, meaning that operations such asmap,flatMapandfilterwork on therightvalue and if you want to work on theleftvalue, then you need to do aswap.For example, you could use
Either<String, Int>to detect whether an input is a string or an number:function tryParseInt(str: string): Either<string, number> { const i = parseInt(value) return isNaN(i) ? Left(str) : Right(i) } const result = tryParseInt("not an int") if (result.isRight()) { console.log(`Increment: ${result.get}`) } else { console.log(`ERROR: could not parse ${result.swap.get}`) }