A
- the optional parameter typepublic abstract class Maybe<A> extends Object implements CoProduct2<Unit,A,Maybe<A>>, MonadError<Unit,A,Maybe<?>>, MonadRec<A,Maybe<?>>, Traversable<A,Maybe<?>>
Optional
, supporting
all the usual suspects like Functor
, Applicative
, Traversable
, etc.Optional
Modifier and Type | Method and Description |
---|---|
Maybe<A> |
catchError(Fn1<? super Unit,? extends Monad<A,Maybe<?>>> recoveryFn)
|
<B> Maybe<B> |
discardL(Applicative<B,Maybe<?>> appB)
Sequence both this
Applicative and appB , discarding this Applicative's
result and returning appB . |
<B> Maybe<A> |
discardR(Applicative<B,Maybe<?>> appB)
Sequence both this
Applicative and appB , discarding appB's result and
returning this Applicative . |
<B> Choice3<Unit,A,B> |
diverge()
Diverge this coproduct by introducing another possible type that it could represent.
|
Maybe<A> |
filter(Fn1<? super A,? extends Boolean> predicate)
If this value is present and satisfies
predicate , return just the value; otherwise,
return nothing . |
<B> Maybe<B> |
flatMap(Fn1<? super A,? extends Monad<B,Maybe<?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.
|
<B> Maybe<B> |
fmap(Fn1<? super A,? extends B> fn)
Covariantly transmute this functor's parameter using the given mapping function.
|
static <A> Maybe<A> |
fromEither(Either<?,A> either)
|
static <A> Maybe<A> |
fromOptional(Optional<? extends A> optional)
|
Choice2<A,Unit> |
invert()
Swap the type parameters.
|
static <A> Maybe<A> |
just(A a)
Lift a non-null value into
Maybe . |
<B> Lazy<Maybe<B>> |
lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,Maybe<?>>> lazyAppFn)
Terminate early if this is a
Nothing ; otherwise, continue the zip . |
static <A> Maybe<A> |
maybe(A a)
Lift a potentially null value into
Maybe . |
static <A> Maybe<A> |
nothing()
Return nothing.
|
A |
orElse(A other)
If the value is present, return it; otherwise, return
other . |
A |
orElseGet(Fn0<A> otherFn0)
If the value is present, return it; otherwise, return the value supplied by
otherSupplier . |
<E extends Throwable> |
orElseThrow(Fn0<? extends E> throwableSupplier)
If the value is present, return it; otherwise, throw the
Throwable supplied by
throwableSupplier . |
Maybe<A> |
peek(Fn1<? super A,? extends IO<?>> effect)
|
Tuple2<Maybe<Unit>,Maybe<A>> |
project()
Project this coproduct onto a product, such that the index in the product that corresponds to this coproduct's
value is present, while the other indices are absent.
|
<B> Maybe<B> |
pure(B b)
Lift the value into the
Maybe monad |
static Pure<Maybe<?>> |
pureMaybe()
|
Maybe<A> |
throwError(Unit unit)
Throw an error value of type
E into the monad . |
<L> Either<L,A> |
toEither(Fn0<L> lFn0)
If this value is absent, return the value supplied by
lSupplier wrapped in Either.left . |
Optional<A> |
toOptional()
Convert to
Optional . |
<B> Maybe<B> |
trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,Maybe<?>>> fn)
Given some operation yielding a
RecursiveResult inside this MonadRec , internally trampoline the
operation until it yields a termination instruction. |
<B,App extends Applicative<?,App>,TravB extends Traversable<B,Maybe<?>>,AppTrav extends Applicative<TravB,App>> |
traverse(Fn1<? super A,? extends Applicative<B,App>> fn,
Fn1<? super TravB,? extends AppTrav> pure)
Apply
fn to each element of this traversable from left to right, and collapse the results into
a single resulting applicative, potentially with the assistance of the applicative's pure function. |
<B> Maybe<B> |
zip(Applicative<Fn1<? super A,? extends B>,Maybe<?>> appFn)
Given another instance of this applicative over a mapping function, "zip" the two instances together using
whatever application semantics the current applicative supports.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
embed, match, projectA, projectB
public final A orElseGet(Fn0<A> otherFn0)
otherSupplier
.otherFn0
- the supplier for the other valuepublic final A orElse(A other)
other
.other
- the other valuepublic final <E extends Throwable> A orElseThrow(Fn0<? extends E> throwableSupplier) throws E extends Throwable
Throwable
supplied by
throwableSupplier
.public final Maybe<A> filter(Fn1<? super A,? extends Boolean> predicate)
predicate
, return just
the value; otherwise,
return nothing
.predicate
- the predicate to apply to the possibly absent valuepublic Maybe<A> throwError(Unit unit)
E
into the monad
.throwError
in interface MonadError<Unit,A,Maybe<?>>
unit
- the error typemonad
public Maybe<A> catchError(Fn1<? super Unit,? extends Monad<A,Maybe<?>>> recoveryFn)
catchError
in interface MonadError<Unit,A,Maybe<?>>
recoveryFn
- the catch functionMonad
public final <L> Either<L,A> toEither(Fn0<L> lFn0)
lSupplier
wrapped in Either.left
.
Otherwise, wrap the value in Either.right
and return it.L
- the left parameter typelFn0
- the supplier for the left valuepublic final <B> Maybe<B> fmap(Fn1<? super A,? extends B> fn)
If the value is present, return just(A)
fn
applied to the value; otherwise, return
nothing()
.
fmap
in interface Applicative<A,Maybe<?>>
fmap
in interface Functor<A,Maybe<?>>
fmap
in interface Monad<A,Maybe<?>>
fmap
in interface MonadError<Unit,A,Maybe<?>>
fmap
in interface MonadRec<A,Maybe<?>>
fmap
in interface Traversable<A,Maybe<?>>
B
- the new parameter typefn
- the mapping functionpublic final <B> Maybe<B> zip(Applicative<Fn1<? super A,? extends B>,Maybe<?>> appFn)
zip
in interface Applicative<A,Maybe<?>>
zip
in interface Monad<A,Maybe<?>>
zip
in interface MonadError<Unit,A,Maybe<?>>
zip
in interface MonadRec<A,Maybe<?>>
B
- the resulting applicative parameter typeappFn
- the other applicative instancepublic <B> Lazy<Maybe<B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,Maybe<?>>> lazyAppFn)
Nothing
; otherwise, continue the zip
.lazyZip
in interface Applicative<A,Maybe<?>>
lazyZip
in interface Monad<A,Maybe<?>>
lazyZip
in interface MonadError<Unit,A,Maybe<?>>
lazyZip
in interface MonadRec<A,Maybe<?>>
B
- the result typelazyAppFn
- the lazy other applicative instanceMaybe
Maybe
,
Either
public final <B> Maybe<B> discardL(Applicative<B,Maybe<?>> appB)
Applicative
and appB
, discarding this Applicative's
result and returning appB
. This is generally useful for sequentially performing side-effects.discardL
in interface Applicative<A,Maybe<?>>
discardL
in interface Monad<A,Maybe<?>>
discardL
in interface MonadError<Unit,A,Maybe<?>>
discardL
in interface MonadRec<A,Maybe<?>>
B
- the type of the returned Applicative's parameterappB
- the other Applicativepublic final <B> Maybe<A> discardR(Applicative<B,Maybe<?>> appB)
Applicative
and appB
, discarding appB's
result and
returning this Applicative
. This is generally useful for sequentially performing side-effects.discardR
in interface Applicative<A,Maybe<?>>
discardR
in interface Monad<A,Maybe<?>>
discardR
in interface MonadError<Unit,A,Maybe<?>>
discardR
in interface MonadRec<A,Maybe<?>>
B
- the type of appB's parameterappB
- the other Applicativepublic final <B> Maybe<B> flatMap(Fn1<? super A,? extends Monad<B,Maybe<?>>> f)
flatMap
in interface Monad<A,Maybe<?>>
flatMap
in interface MonadError<Unit,A,Maybe<?>>
flatMap
in interface MonadRec<A,Maybe<?>>
B
- the resulting monad parameter typef
- the dependent computation over Apublic <B> Maybe<B> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,Maybe<?>>> fn)
RecursiveResult
inside this MonadRec
, internally trampoline the
operation until it yields a termination
instruction.
Stack-safety depends on implementations guaranteeing that the growth of the call stack is a constant factor independent of the number of invocations of the operation. For various examples of how this can be achieved in stereotypical circumstances, see the referenced types.
trampolineM
in interface MonadRec<A,Maybe<?>>
B
- the ultimate resulting carrier typefn
- the function to internally trampolineMonadRec
for a basic implementation
,
for a {@link CoProduct2 coproduct} implementation
,
for an implementation leveraging an already stack-safe {@link Monad#flatMap(Fn1)}
,
for a {@link MonadT monad transformer} implementation
public <B> Choice3<Unit,A,B> diverge()
CoProduct3<String, Integer,
Boolean>
is expected, a CoProduct2<String, Integer>
should suffice.
Generally, we use inheritance to make this a non-issue; however, with coproducts of differing magnitudes, we
cannot guarantee variance compatibility in one direction conveniently at construction time, and in the other
direction, at all. A CoProduct2
could not be a CoProduct3
without specifying all type parameters
that are possible for a CoProduct3
- more specifically, the third possible type - which is not
necessarily known at construction time, or even useful if never used in the context of a CoProduct3
. The
inverse inheritance relationship - CoProduct3
< CoProduct2
- is inherently unsound, as a
CoProduct3
cannot correctly implement CoProduct2.match(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends R>, com.jnape.palatable.lambda.functions.Fn1<? super B, ? extends R>)
, given that the third type C
is always possible.
For this reason, there is a diverge
method supported between all CoProduct
types of
single magnitude difference.
diverge
in interface CoProduct2<Unit,A,Maybe<A>>
B
- the additional possible type of this coproductCoProduct3
<A, B, C>public Tuple2<Maybe<Unit>,Maybe<A>> project()
@Deprecated public final Maybe<A> peek(Fn1<? super A,? extends IO<?>> effect)
consumer
; otherwise, do nothing.effect
- the consumerpublic final <B,App extends Applicative<?,App>,TravB extends Traversable<B,Maybe<?>>,AppTrav extends Applicative<TravB,App>> AppTrav traverse(Fn1<? super A,? extends Applicative<B,App>> fn, Fn1<? super TravB,? extends AppTrav> pure)
Traversable
fn
to each element of this traversable from left to right, and collapse the results into
a single resulting applicative, potentially with the assistance of the applicative's pure function.traverse
in interface Traversable<A,Maybe<?>>
B
- the resulting element typeApp
- the result applicative typeTravB
- this Traversable instance over BAppTrav
- the full inferred resulting type from the traversalfn
- the function to applypure
- the applicative pure functionpublic static <A> Maybe<A> fromEither(Either<?,A> either)
Maybe
from an Either
. If either
is
a right value, wrap the value in a just
and return it; otherwise, return nothing()
.A
- the potential right valueeither
- the either instancepublic static <A> Maybe<A> fromOptional(Optional<? extends A> optional)
A
- the optional parameter typeoptional
- the optionalpublic static <A> Maybe<A> maybe(A a)
Maybe
. If a
is not null, returns just(a)
;
otherwise, returns nothing()
.A
- the value parameter typea
- the potentially null valuepublic static <A> Maybe<A> just(A a)
Maybe
. This differs from maybe(A)
in that the value *must* be
non-null; if it is null, a NullPointerException
is thrown.A
- the value parameter typea
- the non-null valueNullPointerException
- if a is nullpublic static <A> Maybe<A> nothing()
A
- the type of the value, if there was one