Package com.github.tonivade.purefun.type
Interface Either<L,R>
- Type Parameters:
L- type of the left value, negative caseR- type of the right value, positive case
- All Superinterfaces:
Applicable<Either<L,,?>, R> Bindable<Either<L,,?>, R> EitherOf<L,,R> Kind<Either<L,,?>, R> Mappable<Either<L,?>, R>
- All Known Implementing Classes:
Either.Left,Either.Right
public sealed interface Either<L,R>
extends EitherOf<L,R>, Bindable<Either<L,?>,R>, Applicable<Either<L,?>,R>
permits Either.Left<L,R>, Either.Right<L,R>
This type represents two possible values, called left or right. In general left case is considered the negative case, and right case the positive case, so is right biased
Either.left(value): left valueEither.right(value): right value
Usually this type is used to represent a function return type. The left value represents when the function fails, and the right value when function returns normally. It's the functional alternative to exceptions.
Try<T> and Either<Throwable, T> are isomorphisms.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordstatic final recordNested classes/interfaces inherited from interface com.github.tonivade.purefun.core.Applicable
Applicable.Map2<F extends Applicable<F,?>, A, B>, Applicable.Map3<F extends Applicable<F, ?>, A, B, C>, Applicable.Map4<F extends Applicable<F, ?>, A, B, C, D>, Applicable.Map5<F extends Applicable<F, ?>, A, B, C, D, E> -
Method Summary
Modifier and TypeMethodDescriptiondefault <T,U> Either <T, U> flatMapLeft(Function1<? super L, ? extends Either<? extends T, R>> map) default <T> Tdefault Rget()getLeft()Returns the left value if available.default Rdefault Rdefault RgetRight()Returns the right value if available.booleanisLeft()booleanisRight()left()static <L,R> Either <L, R> left(L value) static <L,A, B, Z>
Either<L, Z> map2(Either<L, ? extends A> eitherA, Either<L, ? extends B> eitherB, Function2<? super A, ? super B, ? extends Z> mapper) static <A> Aright()static <L,R> Either <L, R> right(R value) sequence()stream()swap()toOption()default Validation<L, R> Methods inherited from interface com.github.tonivade.purefun.core.Applicable
zip, zipLeft, zipRight, zipWith
-
Method Details
-
left
-
right
-
isLeft
boolean isLeft() -
isRight
boolean isRight() -
getLeft
L getLeft()Returns the left value if available. If not, it throwsNoSuchElementException- Returns:
- the left value
- Throws:
NoSuchElementException- if left value is not available
-
getRight
R getRight()Returns the right value if available. If not, it throwsNoSuchElementException- Returns:
- the right value
- Throws:
NoSuchElementException- if right value is not available
-
get
-
left
-
right
-
swap
-
bimap
-
map
-
ap
- Specified by:
apin interfaceApplicable<L,R>
-
mapLeft
-
flatMap
-
flatMapLeft
-
filter
-
filterNot
-
filterOrElse
-
or
-
orElse
-
getOrElse
-
getOrElseNull
-
getOrElse
-
fold
-
stream
-
sequence
-
toOption
-
toValidation
-
merge
-
map2
-