Interface Monad<A,M extends Monad<?,M>>

Type Parameters:
A - the type of the parameter
M - the unification parameter to more tightly type-constrain Monads to themselves
All Superinterfaces:
Applicative<A,M>, Functor<A,M>
All Known Subinterfaces:
BiMonoidFactory<A,B,C>, BiPredicate<A,B>, BiSemigroupFactory<A,B,C>, Effect<A>, Fn0<A>, Fn1<A,B>, Fn2<A,B,C>, Fn3<A,B,C,D>, Fn4<A,B,C,D,E>, Fn5<A,B,C,D,E,F>, Fn6<A,B,C,D,E,F,G>, Fn7<A,B,C,D,E,F,G,H>, Fn8<A,B,C,D,E,F,G,H,I>, Iso<S,T,A,B>, Iso.Simple<S,A>, Kleisli<A,B,M,MB>, Lens<S,T,A,B>, Lens.Simple<S,A>, MonadError<E,A,M>, MonadReader<R,A,MR>, MonadRec<A,M>, MonadT<M,A,MT,T>, MonadWriter<W,A,MW>, Monoid<A>, MonoidFactory<A,B>, Predicate<A>, Prism<S,T,A,B>, Prism.Simple<S,A>, Schema<Values>, Semigroup<A>, SemigroupFactory<A,B>, TypeSafeKey<A,B>, TypeSafeKey.Simple<A>
All Known Implementing Classes:
$, Absent, AddAll, All, Alter, And, Any, AutoBracket, Between, Both, Bracket, CartesianProduct, CatMaybes, Choice2, Choice2._A, Choice2._B, Choice3, Choice3._A, Choice3._B, Choice3._C, Choice4, Choice4._A, Choice4._B, Choice4._C, Choice4._D, Choice5, Choice5._A, Choice5._B, Choice5._C, Choice5._D, Choice5._E, Choice6, Choice6._A, Choice6._B, Choice6._C, Choice6._D, Choice6._E, Choice6._F, Choice7, Choice7._A, Choice7._B, Choice7._C, Choice7._D, Choice7._E, Choice7._F, Choice7._G, Choice8, Choice8._A, Choice8._B, Choice8._C, Choice8._D, Choice8._E, Choice8._F, Choice8._G, Choice8._H, Clamp, CmpEq, CmpEqBy, CmpEqWith, Coalesce, Collapse, Collapse, Compare, Compose, Compose, Concat, Cons, Const, Constantly, Cycle, Difference, Distinct, Downcast, Drop, DropWhile, Either, Either.Left, Either.Right, EitherT, Empty, Endo, EndoK, Eq, Filter, Find, First, Flatten, FoldLeft, FoldRight, Force, GroupBy, GT, GTBy, GTE, GTEBy, GTEWith, GTWith, Head, Id, Identity, IdentityT, IfThenElse, InGroupsOf, Init, Inits, Intersection, Intersperse, Into, Into1, Into3, Into4, Into5, Into6, Into7, Into8, IO, IO.Compose, Iterate, IterateT, Join, LambdaIterable, Last, Last, Lazy, Lazy.Compose, Lazy.Later, LazyRec, LazyT, LeftAll, LeftAll, LeftAny, LeftAny, LiftA2, LiftA3, LiftA4, LiftA5, LiftA6, LiftA7, LT, LTBy, LTE, LTEBy, LTEWith, LTWith, Magnetize, MagnetizeBy, Map, Market, Matching, Max, MaxBy, MaxWith, Maybe, Maybe.Just, Maybe.Nothing, MaybeT, Merge, Merge, MergeHMaps, MergeMaps, Min, MinBy, MinWith, Not, Occurrences, Or, Over, Partition, Peek, Peek2, Pre, PrependAll, Present, PutAll, RateLimit, Re, ReaderT, RecursiveResult, RecursiveResult.Recurse, RecursiveResult.Terminate, ReduceLeft, ReduceRight, Repeat, Replicate, Reverse, RightAll, RightAll, RightAny, RightAny, RunAll, RunAll, SafeT, ScanLeft, Sequence, Set, SingletonHList, Size, Slide, Snoc, Sort, SortBy, SortWith, Span, State, StateT, Tagged, Tail, Tails, Take, TakeWhile, These, These._A, These._B, These.Both, Times, ToArray, ToCollection, ToMap, Trampoline, Trivial, Try, Try.Failure, Try.Success, Tuple2, Tuple3, Tuple4, Tuple5, Tuple6, Tuple7, Tuple8, Tupler2, Uncons, Under, Unfoldr, Union, Until, Upcast, View, Writer, WriterT, Xor, Zip, ZipWith

public interface Monad<A,M extends Monad<?,M>> extends Applicative<A,M>
Monads are Applicative functors that support a flattening operation to unwrap M<M<A>> -> M<A>. This flattening operation, coupled with Applicative.zip(Applicative), gives rise to flatMap(Fn1), a binding operation that maps the carrier value to a new monad instance in the same category, and then unwraps the outer layer.

In addition to the applicative laws, there are 3 specific monad laws that monads should obey:

  • left identity: m.pure(a).flatMap(fn).equals(fn.apply(a))
  • right identity: m.flatMap(m::pure).equals(m)
  • associativity: m.flatMap(f).flatMap(g).equals(m.flatMap(a -> f.apply(a).flatMap(g)))

For more information, read about Monads.

  • Method Summary

    Modifier and Type
    Method
    Description
    default <B> Monad<B,M>
    Sequence both this Applicative and appB, discarding this Applicative's result and returning appB.
    default <B> Monad<A,M>
    Sequence both this Applicative and appB, discarding appB's result and returning this Applicative.
    <B> Monad<B,M>
    flatMap(Fn1<? super A,? extends Monad<B,M>> f)
    Chain dependent computations that may continue or short-circuit based on previous results.
    default <B> Monad<B,M>
    fmap(Fn1<? super A,? extends B> fn)
    Covariantly transmute this functor's parameter using the given mapping function.
    static <M extends Monad<?, M>, A, MA extends Monad<A, M>>
    MA
    join(Monad<? extends MA,M> mma)
    Convenience static method equivalent to flatMap(id());
    default <B> Lazy<? extends Monad<B,M>>
    lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,M>> lazyAppFn)
    Given a lazy instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
    <B> Monad<B,M>
    pure(B b)
    Lift the value b into this applicative functor.
    default <B> Monad<B,M>
    zip(Applicative<Fn1<? super A,? extends B>,M> 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 interface com.jnape.palatable.lambda.functor.Functor

    coerce
  • Method Details

    • flatMap

      <B> Monad<B,M> flatMap(Fn1<? super A,? extends Monad<B,M>> f)
      Chain dependent computations that may continue or short-circuit based on previous results.
      Type Parameters:
      B - the resulting monad parameter type
      Parameters:
      f - the dependent computation over A
      Returns:
      the new monad instance
    • pure

      <B> Monad<B,M> pure(B b)
      Lift the value b into this applicative functor.
      Specified by:
      pure in interface Applicative<A,M extends Monad<?,M>>
      Type Parameters:
      B - the type of the returned applicative's parameter
      Parameters:
      b - the value
      Returns:
      an instance of this applicative over b
    • fmap

      default <B> Monad<B,M> 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.
      Specified by:
      fmap in interface Applicative<A,M extends Monad<?,M>>
      Specified by:
      fmap in interface Functor<A,M extends Monad<?,M>>
      Type Parameters:
      B - the new parameter type
      Parameters:
      fn - the mapping function
      Returns:
      a functor over B (the new parameter type)
    • zip

      default <B> Monad<B,M> zip(Applicative<Fn1<? super A,? extends B>,M> 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:
      zip in interface Applicative<A,M extends Monad<?,M>>
      Type Parameters:
      B - the resulting applicative parameter type
      Parameters:
      appFn - the other applicative instance
      Returns:
      the mapped applicative
    • lazyZip

      default <B> Lazy<? extends Monad<B,M>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,M>> lazyAppFn)
      Given a lazy instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports. This is useful for applicatives that support lazy evaluation and early termination.
      Specified by:
      lazyZip in interface Applicative<A,M extends Monad<?,M>>
      Type Parameters:
      B - the resulting applicative parameter type
      Parameters:
      lazyAppFn - the lazy other applicative instance
      Returns:
      the mapped applicative
      See Also:
    • discardL

      default <B> Monad<B,M> discardL(Applicative<B,M> appB)
      Sequence both this Applicative and appB, discarding this Applicative's result and returning appB. This is generally useful for sequentially performing side-effects.
      Specified by:
      discardL in interface Applicative<A,M extends Monad<?,M>>
      Type Parameters:
      B - the type of the returned Applicative's parameter
      Parameters:
      appB - the other Applicative
      Returns:
      appB
    • discardR

      default <B> Monad<A,M> discardR(Applicative<B,M> appB)
      Sequence both this Applicative and appB, discarding appB's result and returning this Applicative. This is generally useful for sequentially performing side-effects.
      Specified by:
      discardR in interface Applicative<A,M extends Monad<?,M>>
      Type Parameters:
      B - the type of appB's parameter
      Parameters:
      appB - the other Applicative
      Returns:
      this Applicative
    • join

      static <M extends Monad<?, M>, A, MA extends Monad<A, M>> MA join(Monad<? extends MA,M> mma)
      Convenience static method equivalent to flatMap(id());
      Type Parameters:
      M - the monad type
      A - the nested type parameter
      MA - the nested monad
      Parameters:
      mma - the outer monad
      Returns:
      the nested monad