Class Maybe<A>
- Type Parameters:
A
- the optional parameter type
- All Implemented Interfaces:
CoProduct2<Unit,
,A, Maybe<A>> Applicative<A,
,Maybe<?>> Functor<A,
,Maybe<?>> Monad<A,
,Maybe<?>> MonadError<Unit,
,A, Maybe<?>> MonadRec<A,
,Maybe<?>> Traversable<A,
Maybe<?>>
- Direct Known Subclasses:
Maybe.Just
,Maybe.Nothing
Optional
, supporting
all the usual suspects like Functor
, Applicative
, Traversable
, etc.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static final class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal <B> Maybe
<B> discardL
(Applicative<B, Maybe<?>> appB) Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
.discardR
(Applicative<B, Maybe<?>> appB) Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
.diverge()
Diverge this coproduct by introducing another possible type that it could represent.If this value is present and satisfiespredicate
, returnjust
the value; otherwise, returnnothing
.final <B> Maybe
<B> Chain dependent computations that may continue or short-circuit based on previous results.final <B> Maybe
<B> 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) invert()
Swap the type parameters.static <A> Maybe
<A> just
(A a) Lift a non-null value intoMaybe
.Terminate early if this is aMaybe.Nothing
; otherwise, continue thezip
.static <A> Maybe
<A> maybe
(A a) Lift a potentially null value intoMaybe
.static <A> Maybe
<A> nothing()
Return nothing.final A
If the value is present, return it; otherwise, returnother
.final A
If the value is present, return it; otherwise, return the value supplied byotherSupplier
.orElseThrow
(Fn0<? extends E> throwableSupplier) If the value is present, return it; otherwise, throw theThrowable
supplied bythrowableSupplier
.Deprecated.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.final <B> Maybe
<B> pure
(B b) Lift the value into theMaybe
monadthrowError
(Unit unit) Throw an error value of typeE
into themonad
.If this value is absent, return the value supplied bylSupplier
wrapped inEither.left
.Convert toOptional
.<B> Maybe
<B> trampolineM
(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, Maybe<?>>> fn) Given some operation yielding aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
instruction.final <B,
App extends Applicative<?, App>, TravB extends Traversable<B, Maybe<?>>, AppTrav extends Applicative<TravB, App>>
AppTravtraverse
(Fn1<? super A, ? extends Applicative<B, App>> fn, Fn1<? super TravB, ? extends AppTrav> pure) Applyfn
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.final <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.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.jnape.palatable.lambda.adt.coproduct.CoProduct2
embed, match, projectA, projectB
-
Constructor Details
-
Maybe
private Maybe()
-
-
Method Details
-
orElseGet
If the value is present, return it; otherwise, return the value supplied byotherSupplier
.- Parameters:
otherFn0
- the supplier for the other value- Returns:
- this value, or the supplied other value
-
orElse
If the value is present, return it; otherwise, returnother
.- Parameters:
other
- the other value- Returns:
- this value, or the other value
-
orElseThrow
If the value is present, return it; otherwise, throw theThrowable
supplied bythrowableSupplier
.- Type Parameters:
E
- the Throwable type- Parameters:
throwableSupplier
- the supplier of the potentially thrownThrowable
- Returns:
- the value, if present
- Throws:
E
- the throwable, if the value is absent
-
filter
If this value is present and satisfiespredicate
, returnjust
the value; otherwise, returnnothing
.- Parameters:
predicate
- the predicate to apply to the possibly absent value- Returns:
- maybe the present value that satisfied the predicate
-
throwError
Throw an error value of typeE
into themonad
.- Specified by:
throwError
in interfaceMonadError<Unit,
A, Maybe<?>> - Parameters:
unit
- the error type- Returns:
- the
monad
-
catchError
- Specified by:
catchError
in interfaceMonadError<Unit,
A, Maybe<?>> - Parameters:
recoveryFn
- the catch function- Returns:
- the recovered
Monad
-
toEither
If this value is absent, return the value supplied bylSupplier
wrapped inEither.left
. Otherwise, wrap the value inEither.right
and return it.- Type Parameters:
L
- the left parameter type- Parameters:
lFn0
- the supplier for the left value- Returns:
- this value wrapped in an Either.right, or an Either.left around the result of lSupplier
-
toOptional
Convert toOptional
.- Returns:
- the Optional
-
pure
Lift the value into theMaybe
monad -
fmap
Covariantly transmute this functor's parameter using the given mapping function. Generally this method is specialized to return an instance of the class implementing Functor.If the value is present, return
just(A)
fn
applied to the value; otherwise, returnnothing()
.- Specified by:
fmap
in interfaceApplicative<A,
Maybe<?>> - Specified by:
fmap
in interfaceFunctor<A,
Maybe<?>> - Specified by:
fmap
in interfaceMonad<A,
Maybe<?>> - Specified by:
fmap
in interfaceMonadError<Unit,
A, Maybe<?>> - Specified by:
fmap
in interfaceMonadRec<A,
Maybe<?>> - Specified by:
fmap
in interfaceTraversable<A,
Maybe<?>> - Type Parameters:
B
- the new parameter type- Parameters:
fn
- the mapping function- Returns:
- a functor over B (the new parameter type)
-
zip
Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.- Specified by:
zip
in interfaceApplicative<A,
Maybe<?>> - Specified by:
zip
in interfaceMonad<A,
Maybe<?>> - Specified by:
zip
in interfaceMonadError<Unit,
A, Maybe<?>> - Specified by:
zip
in interfaceMonadRec<A,
Maybe<?>> - Type Parameters:
B
- the resulting applicative parameter type- Parameters:
appFn
- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public <B> Lazy<Maybe<B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A, ? extends B>, Maybe<?>>> lazyAppFn) Terminate early if this is aMaybe.Nothing
; otherwise, continue thezip
.- Specified by:
lazyZip
in interfaceApplicative<A,
Maybe<?>> - Specified by:
lazyZip
in interfaceMonad<A,
Maybe<?>> - Specified by:
lazyZip
in interfaceMonadError<Unit,
A, Maybe<?>> - Specified by:
lazyZip
in interfaceMonadRec<A,
Maybe<?>> - Type Parameters:
B
- the result type- Parameters:
lazyAppFn
- the lazy other applicative instance- Returns:
- the zipped
Maybe
- See Also:
-
discardL
Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
. This is generally useful for sequentially performing side-effects.- Specified by:
discardL
in interfaceApplicative<A,
Maybe<?>> - Specified by:
discardL
in interfaceMonad<A,
Maybe<?>> - Specified by:
discardL
in interfaceMonadError<Unit,
A, Maybe<?>> - Specified by:
discardL
in interfaceMonadRec<A,
Maybe<?>> - Type Parameters:
B
- the type of the returned Applicative's parameter- Parameters:
appB
- the other Applicative- Returns:
- appB
-
discardR
Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
. This is generally useful for sequentially performing side-effects.- Specified by:
discardR
in interfaceApplicative<A,
Maybe<?>> - Specified by:
discardR
in interfaceMonad<A,
Maybe<?>> - Specified by:
discardR
in interfaceMonadError<Unit,
A, Maybe<?>> - Specified by:
discardR
in interfaceMonadRec<A,
Maybe<?>> - Type Parameters:
B
- the type of appB's parameter- Parameters:
appB
- the other Applicative- Returns:
- this Applicative
-
flatMap
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMap
in interfaceMonad<A,
Maybe<?>> - Specified by:
flatMap
in interfaceMonadError<Unit,
A, Maybe<?>> - Specified by:
flatMap
in interfaceMonadRec<A,
Maybe<?>> - Type Parameters:
B
- the resulting monad parameter type- Parameters:
f
- the dependent computation over A- Returns:
- the new monad instance
-
trampolineM
public <B> Maybe<B> trampolineM(Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, Maybe<?>>> fn) Given some operation yielding aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
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.
- Specified by:
trampolineM
in interfaceMonadRec<A,
Maybe<?>> - Type Parameters:
B
- the ultimate resulting carrier type- Parameters:
fn
- the function to internally trampoline- Returns:
- the trampolined
MonadRec
- See Also:
-
diverge
Diverge this coproduct by introducing another possible type that it could represent. As no morphisms can be provided mapping current types to the new type, this operation merely acts as a convenience method to allow the use of a more convergent coproduct with a more divergent one; that is, if aCoProduct3<String, Integer, Boolean>
is expected, aCoProduct2<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 aCoProduct3
without specifying all type parameters that are possible for aCoProduct3
- more specifically, the third possible type - which is not necessarily known at construction time, or even useful if never used in the context of aCoProduct3
. The inverse inheritance relationship -CoProduct3
<CoProduct2
- is inherently unsound, as aCoProduct3
cannot correctly implementCoProduct2.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 typeC
is always possible.For this reason, there is a
diverge
method supported between allCoProduct
types of single magnitude difference.- Specified by:
diverge
in interfaceCoProduct2<Unit,
A, Maybe<A>> - Type Parameters:
B
- the additional possible type of this coproduct- Returns:
- a
CoProduct3
<A, B, C>
-
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. -
invert
Swap the type parameters. -
peek
If this value is present, accept it byconsumer
; otherwise, do nothing.- Parameters:
effect
- the consumer- Returns:
- the same Maybe instance
-
traverse
public final <B,App extends Applicative<?, AppTrav traverseApp>, TravB extends Traversable<B, Maybe<?>>, AppTrav extends Applicative<TravB, App>> (Fn1<? super A, ? extends Applicative<B, App>> fn, Fn1<? super TravB, ? extends AppTrav> pure) Description copied from interface:Traversable
Applyfn
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.- Specified by:
traverse
in interfaceTraversable<A,
Maybe<?>> - Type Parameters:
B
- the resulting element typeApp
- the result applicative typeTravB
- this Traversable instance over BAppTrav
- the full inferred resulting type from the traversal- Parameters:
fn
- the function to applypure
- the applicative pure function- Returns:
- the traversed Traversable, wrapped inside an applicative
-
fromEither
Convenience static factory method for creating aMaybe
from anEither
. Ifeither
is a right value, wrap the value in ajust
and return it; otherwise, returnnothing()
.- Type Parameters:
A
- the potential right value- Parameters:
either
- the either instance- Returns:
- "Just" the right value, or nothing
-
fromOptional
- Type Parameters:
A
- the optional parameter type- Parameters:
optional
- the optional- Returns:
- the equivalent Maybe instance
-
maybe
Lift a potentially null value intoMaybe
. Ifa
is not null, returnsjust(a)
; otherwise, returnsnothing()
.- Type Parameters:
A
- the value parameter type- Parameters:
a
- the potentially null value- Returns:
- "Just" the value, or nothing
-
just
Lift a non-null value intoMaybe
. This differs frommaybe(A)
in that the value *must* be non-null; if it is null, aNullPointerException
is thrown.- Type Parameters:
A
- the value parameter type- Parameters:
a
- the non-null value- Returns:
- "Just" the value
- Throws:
NullPointerException
- if a is null
-
nothing
Return nothing.- Type Parameters:
A
- the type of the value, if there was one- Returns:
- nothing
-
pureMaybe
- Returns:
- the
Pure
instance
-
matching
into anIO
and explicitly running it