Class Identity<A>

java.lang.Object
com.jnape.palatable.lambda.functor.builtin.Identity<A>
Type Parameters:
A - the value type
All Implemented Interfaces:
Applicative<A,Identity<?>>, Functor<A,Identity<?>>, Monad<A,Identity<?>>, MonadRec<A,Identity<?>>, Traversable<A,Identity<?>>

public final class Identity<A> extends Object implements MonadRec<A,Identity<?>>, Traversable<A,Identity<?>>
A functor over some value of type A that can be mapped over and retrieved later.
  • Field Details

    • a

      private final A a
  • Constructor Details

    • Identity

      public Identity(A a)
  • Method Details

    • runIdentity

      public A runIdentity()
      Retrieve the value.
      Returns:
      the value
    • flatMap

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

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

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

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

      public <B> Lazy<Identity<B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,Identity<?>>> 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,Identity<?>>
      Specified by:
      lazyZip in interface Monad<A,Identity<?>>
      Specified by:
      lazyZip in interface MonadRec<A,Identity<?>>
      Type Parameters:
      B - the resulting applicative parameter type
      Parameters:
      lazyAppFn - the lazy other applicative instance
      Returns:
      the mapped applicative
      See Also:
    • discardL

      public <B> Identity<B> discardL(Applicative<B,Identity<?>> 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,Identity<?>>
      Specified by:
      discardL in interface Monad<A,Identity<?>>
      Specified by:
      discardL in interface MonadRec<A,Identity<?>>
      Type Parameters:
      B - the type of the returned Applicative's parameter
      Parameters:
      appB - the other Applicative
      Returns:
      appB
    • discardR

      public <B> Identity<A> discardR(Applicative<B,Identity<?>> 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,Identity<?>>
      Specified by:
      discardR in interface Monad<A,Identity<?>>
      Specified by:
      discardR in interface MonadRec<A,Identity<?>>
      Type Parameters:
      B - the type of appB's parameter
      Parameters:
      appB - the other Applicative
      Returns:
      this Applicative
    • traverse

      public <B, App extends Applicative<?, App>, TravB extends Traversable<B, Identity<?>>, AppTrav extends Applicative<TravB, App>> AppTrav 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.
      Specified by:
      traverse in interface Traversable<A,Identity<?>>
      Type Parameters:
      B - the resulting element type
      App - the result applicative type
      TravB - this Traversable instance over B
      AppTrav - the full inferred resulting type from the traversal
      Parameters:
      fn - the function to apply
      pure - the applicative pure function
      Returns:
      the traversed Traversable, wrapped inside an applicative
    • trampolineM

      public <B> Identity<B> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,Identity<?>>> fn)
      Given some operation yielding a 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.

      Specified by:
      trampolineM in interface MonadRec<A,Identity<?>>
      Type Parameters:
      B - the ultimate resulting carrier type
      Parameters:
      fn - the function to internally trampoline
      Returns:
      the trampolined MonadRec
      See Also:
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • pureIdentity

      public static Pure<Identity<?>> pureIdentity()
      The canonical Pure instance for Identity.
      Returns:
      the Pure instance