Class/Object

cats.data

AndThen

Related Docs: object AndThen | package data

Permalink

sealed abstract class AndThen[-T, +R] extends (T) ⇒ R with Product with Serializable

A function type of a single input that can do function composition (via andThen and compose) in constant stack space with amortized linear time application (in the number of constituent functions).

Example:

val seed = AndThen((x: Int) => x + 1))
val f = (0 until 10000).foldLeft(seed)((acc, _) => acc.andThen(_ + 1))

// This should not trigger stack overflow ;-)
f(0)

This can be used to build stack safe data structures that make use of lambdas. The perfect candidates for usage with AndThen are the data structures using a signature like this (where F[_] is a monadic type):

A => F[B]

As an example, if we described this data structure, the naive solution for that map is stack unsafe:

case class Resource[F[_], A, B](
  acquire: F[A],
  use: A => F[B],
  release: A => F[Unit]) {

  def flatMap[C](f: B => C)(implicit F: Functor[F]): Resource[F, A, C] = {
    Resource(
      ra.acquire,
      // Stack Unsafe!
      a => ra.use(a).map(f),
      ra.release)
  }
}

To describe a flatMap operation for this data type, AndThen can save the day:

def flatMap[C](f: B => C)(implicit F: Functor[F]): Resource[F, A, C] = {
  Resource(
    ra.acquire,
    AndThen(ra.use).andThen(_.map(f)),
    ra.release)
}
Linear Supertypes
Serializable, Product, Equals, (T) ⇒ R, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. AndThen
  2. Serializable
  3. Product
  4. Equals
  5. Function1
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def canEqual(that: Any): Boolean

    Permalink
    Definition Classes
    Equals
  2. abstract def productArity: Int

    Permalink
    Definition Classes
    Product
  3. abstract def productElement(n: Int): Any

    Permalink
    Definition Classes
    Product

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def andThen[A](g: (R) ⇒ A): AndThen[T, A]

    Permalink
    Definition Classes
    AndThen → Function1
  5. final def apply(a: T): R

    Permalink
    Definition Classes
    AndThen → Function1
  6. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. def compose[A](g: (A) ⇒ T): AndThen[A, R]

    Permalink
    Definition Classes
    AndThen → Function1
  9. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  10. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  11. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  12. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  13. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  14. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  15. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  16. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  18. def productIterator: Iterator[Any]

    Permalink
    Definition Classes
    Product
  19. def productPrefix: String

    Permalink
    Definition Classes
    Product
  20. final def rotateAccum[E](_right: AndThen[R, E]): AndThen[T, E]

    Permalink
    Attributes
    protected
  21. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  22. def toString(): String

    Permalink
    Definition Classes
    AndThen → Function1 → AnyRef → Any
  23. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  24. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from (T) ⇒ R

Inherited from AnyRef

Inherited from Any

Ungrouped