Package fj.data
Class Either<A,B>
- java.lang.Object
-
- fj.data.Either<A,B>
-
- Direct Known Subclasses:
Either.Left
,Either.Right
public abstract class Either<A,B> extends java.lang.Object
TheEither
type represents a value of one of two possible types (a disjoint union). The data constructors;Left
andRight
represent the two possible values. TheEither
type is often used as an alternative toscala.Option
whereLeft
represents failure (by convention) andRight
is akin toSome
.- Version:
- %build.number%
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Either.Left<A,B>
static class
Either.LeftProjection<A,B>
A left projection of an either value.private static class
Either.Right<A,B>
static class
Either.RightProjection<A,B>
A right projection of an either value.
-
Constructor Summary
Constructors Modifier Constructor Description private
Either()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <X,Y>
Either<X,Y>bimap(F<A,X> left, F<B,Y> right)
Map the given functions across the appropriate side.abstract <X> X
either(F<A,X> left, F<B,X> right)
The catamorphism for either.static <A,B,X>
F<Either<A,B>,X>either_(F<A,X> left, F<B,X> right)
First class catamorphism for either.boolean
equals(java.lang.Object other)
int
hashCode()
static <A,B>
Either<A,B>iif(boolean c, F0<B> right, F0<A> left)
If the condition satisfies, return the given B in right, otherwise, return the given A in left.abstract boolean
isLeft()
Returnstrue
if this either is a left,false
otherwise.abstract boolean
isRight()
Returnstrue
if this either is a right,false
otherwise.static <A,B>
Either<A,B>joinLeft(Either<Either<A,B>,B> e)
Joins an either through left.static <A,B>
Either<A,B>joinRight(Either<A,Either<A,B>> e)
Joins an either through right.Either.LeftProjection<A,B>
left()
Projects this either as a left.static <A,B>
Either<A,B>left(A a)
Construct a left value of either.static <A,B>
F<A,Either<A,B>>left_()
A function that constructs a left value of either.<X> Either<X,B>
leftMap(F<A,X> f)
Map the given function across this either's left projection.static <A,B,X>
F<F<A,X>,F<Either<A,B>,Either<X,B>>>leftMap_()
Return a function that maps a given function across this either's left projection.static <A,B>
List<A>lefts(List<Either<A,B>> es)
Returns all the left values in the given list.static <A> A
reduce(Either<A,A> e)
Takes anEither
to its contained value within left or right.Either.RightProjection<A,B>
right()
Projects this either as a right.static <A,B>
Either<A,B>right(B b)
Construct a right value of either.static <A,B>
F<B,Either<A,B>>right_()
A function that constructs a right value of either.<X> Either<A,X>
rightMap(F<B,X> f)
Map the given function across this either's right.static <A,B,X>
F<F<B,X>,F<Either<A,B>,Either<A,X>>>rightMap_()
Return a function that maps a given function across this either's right projection.static <A,B>
List<B>rights(List<Either<A,B>> es)
Returns all the right values in the given list.static <A,X>
Either<List<A>,X>sequenceLeft(List<Either<A,X>> a)
Sequences through the left side of the either monad with a list of values.static <B,X>
Either<X,List<B>>sequenceRight(List<Either<X,B>> a)
Sequences through the right side of the either monad with a list of values.Either<B,A>
swap()
If this is a left, then return the left value in right, or vice versa.java.lang.String
toString()
<C> IO<Either<C,B>>
traverseIOLeft(F<A,IO<C>> f)
Traversable instance of LeftProjection of Either for IO.<C> IO<Either<A,C>>
traverseIORight(F<B,IO<C>> f)
Traversable instance of RightProjection of Either for IO.<C> List<Either<C,B>>
traverseListLeft(F<A,List<C>> f)
Traversable instance of LeftProjection of Either for List.<C> List<Either<A,C>>
traverseListRight(F<B,List<C>> f)
Traversable instance of RightProjection of Either for List.<C> Option<Either<C,B>>
traverseOptionLeft(F<A,Option<C>> f)
Traversable instance of LeftProjection of Either for Option.<C> Option<Either<A,C>>
traverseOptionRight(F<B,Option<C>> f)
Traversable instance of RightProjection of Either for Option.<C> Stream<Either<C,B>>
traverseStreamLeft(F<A,Stream<C>> f)
Traversable instance of LeftProjection of Either for Stream.<C> Stream<Either<A,C>>
traverseStreamRight(F<B,Stream<C>> f)
Traversable instance of RightProjection of Either for Stream.
-
-
-
Method Detail
-
left
public final Either.LeftProjection<A,B> left()
Projects this either as a left.- Returns:
- A left projection of this either.
-
right
public final Either.RightProjection<A,B> right()
Projects this either as a right.- Returns:
- A right projection of this either.
-
isLeft
public abstract boolean isLeft()
Returnstrue
if this either is a left,false
otherwise.- Returns:
true
if this either is a left,false
otherwise.
-
isRight
public abstract boolean isRight()
Returnstrue
if this either is a right,false
otherwise.- Returns:
true
if this either is a right,false
otherwise.
-
either
public abstract <X> X either(F<A,X> left, F<B,X> right)
The catamorphism for either. Folds over this either breaking into left or right.- Parameters:
left
- The function to call if this is left.right
- The function to call if this is right.- Returns:
- The reduced value.
-
bimap
public final <X,Y> Either<X,Y> bimap(F<A,X> left, F<B,Y> right)
Map the given functions across the appropriate side.- Parameters:
left
- The function to map if this is left.right
- The function to map if this is right.- Returns:
- A new either value after mapping with the appropriate function applied.
-
equals
public final boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public final int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
swap
public final Either<B,A> swap()
If this is a left, then return the left value in right, or vice versa.- Returns:
- The value of this either swapped to the opposing side.
-
left
public static <A,B> Either<A,B> left(A a)
Construct a left value of either.- Parameters:
a
- The value underlying the either.- Returns:
- A left value of either.
-
left_
public static <A,B> F<A,Either<A,B>> left_()
A function that constructs a left value of either.- Returns:
- A function that constructs a left value of either.
-
right_
public static <A,B> F<B,Either<A,B>> right_()
A function that constructs a right value of either.- Returns:
- A function that constructs a right value of either.
-
right
public static <A,B> Either<A,B> right(B b)
Construct a right value of either.- Parameters:
b
- The value underlying the either.- Returns:
- A right value of either.
-
either_
public static <A,B,X> F<Either<A,B>,X> either_(F<A,X> left, F<B,X> right)
First class catamorphism for either. Folds over this either breaking into left or right.- Parameters:
left
- The function to call if this is left.right
- The function to call if this is right.- Returns:
- The reducing function.
-
leftMap
public final <X> Either<X,B> leftMap(F<A,X> f)
Map the given function across this either's left projection.- Type Parameters:
X
- the type of the function output- Parameters:
f
- the given function- Returns:
- the either
-
leftMap_
public static <A,B,X> F<F<A,X>,F<Either<A,B>,Either<X,B>>> leftMap_()
Return a function that maps a given function across this either's left projection.- Type Parameters:
A
- the type of the right valueB
- the type of the left valueX
- the type of the function output- Returns:
- A function that maps another function across an either's left projection.
-
rightMap
public final <X> Either<A,X> rightMap(F<B,X> f)
Map the given function across this either's right.- Type Parameters:
X
- the type of the function output- Parameters:
f
- the given function- Returns:
- the either
-
rightMap_
public static <A,B,X> F<F<B,X>,F<Either<A,B>,Either<A,X>>> rightMap_()
Return a function that maps a given function across this either's right projection.- Type Parameters:
A
- the type of the right valueB
- the type of the left valueX
- the type of the function output- Returns:
- A function that maps another function across an either's right projection.
-
joinLeft
public static <A,B> Either<A,B> joinLeft(Either<Either<A,B>,B> e)
Joins an either through left.- Parameters:
e
- The either of either to join.- Returns:
- An either after joining.
-
joinRight
public static <A,B> Either<A,B> joinRight(Either<A,Either<A,B>> e)
Joins an either through right.- Parameters:
e
- The either of either to join.- Returns:
- An either after joining.
-
sequenceLeft
public static <A,X> Either<List<A>,X> sequenceLeft(List<Either<A,X>> a)
Sequences through the left side of the either monad with a list of values.- Parameters:
a
- The list of values to sequence with the either monad.- Returns:
- A sequenced value.
- See Also:
List.sequenceEitherLeft(fj.data.List<fj.data.Either<B, R>>)
-
sequenceRight
public static <B,X> Either<X,List<B>> sequenceRight(List<Either<X,B>> a)
Sequences through the right side of the either monad with a list of values.- Parameters:
a
- The list of values to sequence with the either monad.- Returns:
- A sequenced value.
- See Also:
List.sequenceEither(fj.data.List<fj.data.Either<L, B>>)
,List.sequenceEitherRight(fj.data.List<fj.data.Either<L, B>>)
-
traverseListRight
public final <C> List<Either<A,C>> traverseListRight(F<B,List<C>> f)
Traversable instance of RightProjection of Either for List.- Returns:
- traversed value
-
traverseListLeft
public final <C> List<Either<C,B>> traverseListLeft(F<A,List<C>> f)
Traversable instance of LeftProjection of Either for List.- Returns:
- traversed value
-
traverseIORight
public final <C> IO<Either<A,C>> traverseIORight(F<B,IO<C>> f)
Traversable instance of RightProjection of Either for IO.- Returns:
- traversed value
-
traverseIOLeft
public final <C> IO<Either<C,B>> traverseIOLeft(F<A,IO<C>> f)
Traversable instance of LeftProjection of Either for IO.- Returns:
- traversed value
-
traverseOptionRight
public final <C> Option<Either<A,C>> traverseOptionRight(F<B,Option<C>> f)
Traversable instance of RightProjection of Either for Option.- Returns:
- traversed value
-
traverseOptionLeft
public final <C> Option<Either<C,B>> traverseOptionLeft(F<A,Option<C>> f)
Traversable instance of LeftProjection of Either for Option.- Returns:
- traversed value
-
traverseStreamRight
public final <C> Stream<Either<A,C>> traverseStreamRight(F<B,Stream<C>> f)
Traversable instance of RightProjection of Either for Stream.- Returns:
- traversed value
-
traverseStreamLeft
public final <C> Stream<Either<C,B>> traverseStreamLeft(F<A,Stream<C>> f)
Traversable instance of LeftProjection of Either for Stream.- Returns:
- traversed value
-
reduce
public static <A> A reduce(Either<A,A> e)
Takes anEither
to its contained value within left or right.- Parameters:
e
- The either to reduce.- Returns:
- An
Either
to its contained value within left or right.
-
iif
public static <A,B> Either<A,B> iif(boolean c, F0<B> right, F0<A> left)
If the condition satisfies, return the given B in right, otherwise, return the given A in left.- Parameters:
c
- The condition to test.right
- The right value to use if the condition satisfies.left
- The left value to use if the condition does not satisfy.- Returns:
- A constructed either based on the given condition.
-
lefts
public static <A,B> List<A> lefts(List<Either<A,B>> es)
Returns all the left values in the given list.- Parameters:
es
- The list of possible left values.- Returns:
- All the left values in the given list.
-
rights
public static <A,B> List<B> rights(List<Either<A,B>> es)
Returns all the right values in the given list.- Parameters:
es
- The list of possible right values.- Returns:
- All the right values in the given list.
-
toString
public final java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-