Class Maybe<A>
- java.lang.Object
-
- com.jnape.palatable.lambda.adt.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
public abstract class Maybe<A> extends java.lang.Object implements CoProduct2<Unit,A,Maybe<A>>, MonadError<Unit,A,Maybe<?>>, MonadRec<A,Maybe<?>>, Traversable<A,Maybe<?>>
The optional type, representing a potentially absent value. This is lambda's analog ofOptional, supporting all the usual suspects likeFunctor,Applicative,Traversable, etc.- See Also:
Optional
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMaybe.Just<A>private static classMaybe.Nothing<A>
-
Constructor Summary
Constructors Modifier Constructor Description privateMaybe()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Maybe<A>catchError(Fn1<? super Unit,? extends Monad<A,Maybe<?>>> recoveryFn)<B> Maybe<B>discardL(Applicative<B,Maybe<?>> appB)Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.<B> Maybe<A>discardR(Applicative<B,Maybe<?>> appB)Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.<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 java.lang.Boolean> predicate)If this value is present and satisfiespredicate, returnjustthe value; otherwise, returnnothing.<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(java.util.Optional<? extends A> optional)Convenience static factory method for creating aMaybefrom anOptional.Choice2<A,Unit>invert()Swap the type parameters.static <A> Maybe<A>just(A a)Lift a non-null value intoMaybe.<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.static <A> Maybe<A>maybe(A a)Lift a potentially null value intoMaybe.static <A> Maybe<A>nothing()Return nothing.AorElse(A other)If the value is present, return it; otherwise, returnother.AorElseGet(Fn0<A> otherFn0)If the value is present, return it; otherwise, return the value supplied byotherSupplier.<E extends java.lang.Throwable>
AorElseThrow(Fn0<? extends E> throwableSupplier)If the value is present, return it; otherwise, throw theThrowablesupplied bythrowableSupplier.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 theMaybemonadstatic Pure<Maybe<?>>pureMaybe()Maybe<A>throwError(Unit unit)Throw an error value of typeEinto themonad.<L> Either<L,A>toEither(Fn0<L> lFn0)If this value is absent, return the value supplied bylSupplierwrapped inEither.left.java.util.Optional<A>toOptional()Convert toOptional.<B> Maybe<B>trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,Maybe<?>>> fn)Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.<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)Applyfnto 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.-
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
-
-
-
-
Method Detail
-
orElseGet
public final A orElseGet(Fn0<A> otherFn0)
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
public final A orElse(A other)
If the value is present, return it; otherwise, returnother.- Parameters:
other- the other value- Returns:
- this value, or the other value
-
orElseThrow
public final <E extends java.lang.Throwable> A orElseThrow(Fn0<? extends E> throwableSupplier) throws E extends java.lang.Throwable
If the value is present, return it; otherwise, throw theThrowablesupplied 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 absentE extends java.lang.Throwable
-
filter
public final Maybe<A> filter(Fn1<? super A,? extends java.lang.Boolean> predicate)
If this value is present and satisfiespredicate, returnjustthe value; otherwise, returnnothing.- Parameters:
predicate- the predicate to apply to the possibly absent value- Returns:
- maybe the present value that satisfied the predicate
-
throwError
public Maybe<A> throwError(Unit unit)
Throw an error value of typeEinto themonad.- Specified by:
throwErrorin interfaceMonadError<Unit,A,Maybe<?>>- Parameters:
unit- the error type- Returns:
- the
monad
-
catchError
public Maybe<A> catchError(Fn1<? super Unit,? extends Monad<A,Maybe<?>>> recoveryFn)
- Specified by:
catchErrorin interfaceMonadError<Unit,A,Maybe<?>>- Parameters:
recoveryFn- the catch function- Returns:
- the recovered
Monad
-
toEither
public final <L> Either<L,A> toEither(Fn0<L> lFn0)
If this value is absent, return the value supplied bylSupplierwrapped inEither.left. Otherwise, wrap the value inEither.rightand 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
public final java.util.Optional<A> toOptional()
Convert toOptional.- Returns:
- the Optional
-
pure
public final <B> Maybe<B> pure(B b)
Lift the value into theMaybemonad- Specified by:
purein interfaceApplicative<A,Maybe<?>>- Specified by:
purein interfaceMonad<A,Maybe<?>>- Specified by:
purein interfaceMonadError<Unit,A,Maybe<?>>- Specified by:
purein interfaceMonadRec<A,Maybe<?>>- Type Parameters:
B- the value type- Parameters:
b- the value- Returns:
- Just b
-
fmap
public final <B> Maybe<B> fmap(Fn1<? super A,? extends B> fn)
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)fnapplied to the value; otherwise, returnnothing().- Specified by:
fmapin interfaceApplicative<A,Maybe<?>>- Specified by:
fmapin interfaceFunctor<A,Maybe<?>>- Specified by:
fmapin interfaceMonad<A,Maybe<?>>- Specified by:
fmapin interfaceMonadError<Unit,A,Maybe<?>>- Specified by:
fmapin interfaceMonadRec<A,Maybe<?>>- Specified by:
fmapin 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
public 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.- Specified by:
zipin interfaceApplicative<A,Maybe<?>>- Specified by:
zipin interfaceMonad<A,Maybe<?>>- Specified by:
zipin interfaceMonadError<Unit,A,Maybe<?>>- Specified by:
zipin 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:
lazyZipin interfaceApplicative<A,Maybe<?>>- Specified by:
lazyZipin interfaceMonad<A,Maybe<?>>- Specified by:
lazyZipin interfaceMonadError<Unit,A,Maybe<?>>- Specified by:
lazyZipin interfaceMonadRec<A,Maybe<?>>- Type Parameters:
B- the result type- Parameters:
lazyAppFn- the lazy other applicative instance- Returns:
- the zipped
Maybe - See Also:
Maybe,Either
-
discardL
public final <B> Maybe<B> discardL(Applicative<B,Maybe<?>> appB)
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects.- Specified by:
discardLin interfaceApplicative<A,Maybe<?>>- Specified by:
discardLin interfaceMonad<A,Maybe<?>>- Specified by:
discardLin interfaceMonadError<Unit,A,Maybe<?>>- Specified by:
discardLin interfaceMonadRec<A,Maybe<?>>- Type Parameters:
B- the type of the returned Applicative's parameter- Parameters:
appB- the other Applicative- Returns:
- appB
-
discardR
public final <B> Maybe<A> discardR(Applicative<B,Maybe<?>> appB)
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects.- Specified by:
discardRin interfaceApplicative<A,Maybe<?>>- Specified by:
discardRin interfaceMonad<A,Maybe<?>>- Specified by:
discardRin interfaceMonadError<Unit,A,Maybe<?>>- Specified by:
discardRin interfaceMonadRec<A,Maybe<?>>- Type Parameters:
B- the type of appB's parameter- Parameters:
appB- the other Applicative- Returns:
- this Applicative
-
flatMap
public final <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.- Specified by:
flatMapin interfaceMonad<A,Maybe<?>>- Specified by:
flatMapin interfaceMonadError<Unit,A,Maybe<?>>- Specified by:
flatMapin 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 aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.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:
trampolineMin interfaceMonadRec<A,Maybe<?>>- Type Parameters:
B- the ultimate resulting carrier type- Parameters:
fn- the function to internally trampoline- Returns:
- the trampolined
MonadRec - See Also:
for a basic implementation,for a implementation,for an implementation leveraging an already stack-safe,for a implementation
-
diverge
public <B> Choice3<Unit,A,B> 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
CoProduct2could not be aCoProduct3without 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 aCoProduct3cannot 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 typeCis always possible.For this reason, there is a
divergemethod supported between allCoProducttypes of single magnitude difference.- Specified by:
divergein interfaceCoProduct2<Unit,A,Maybe<A>>- Type Parameters:
B- the additional possible type of this coproduct- Returns:
- a
CoProduct3<A, B, C>
-
project
public 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.
-
peek
@Deprecated public final Maybe<A> peek(Fn1<? super A,? extends IO<?>> effect)
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<?,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)
Description copied from interface:TraversableApplyfnto 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:
traversein 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
public static <A> Maybe<A> fromEither(Either<?,A> either)
Convenience static factory method for creating aMaybefrom anEither. Ifeitheris a right value, wrap the value in ajustand return it; otherwise, returnnothing().- Type Parameters:
A- the potential right value- Parameters:
either- the either instance- Returns:
- "Just" the right value, or nothing
-
fromOptional
public static <A> Maybe<A> fromOptional(java.util.Optional<? extends A> optional)
Convenience static factory method for creating aMaybefrom anOptional.- Type Parameters:
A- the optional parameter type- Parameters:
optional- the optional- Returns:
- the equivalent Maybe instance
-
maybe
public static <A> Maybe<A> maybe(A a)
Lift a potentially null value intoMaybe. Ifais 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
public static <A> Maybe<A> just(A a)
Lift a non-null value intoMaybe. This differs frommaybe(A)in that the value *must* be non-null; if it is null, aNullPointerExceptionis thrown.- Type Parameters:
A- the value parameter type- Parameters:
a- the non-null value- Returns:
- "Just" the value
- Throws:
java.lang.NullPointerException- if a is null
-
nothing
public static <A> Maybe<A> nothing()
Return nothing.- Type Parameters:
A- the type of the value, if there was one- Returns:
- nothing
-
-