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)
}
Source
AndThen.scala
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
    Definition Classes
    Equals
  2. abstract def productArity: Int
    Definition Classes
    Product
  3. abstract def productElement(n: Int): Any
    Definition Classes
    Product

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def andThen[A](g: (R) ⇒ A): AndThen[T, A]
    Definition Classes
    AndThen → Function1
  5. final def apply(a: T): R
    Definition Classes
    AndThen → Function1
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  8. def compose[A](g: (A) ⇒ T): AndThen[A, R]
    Definition Classes
    AndThen → Function1
  9. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  10. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  11. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  12. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  13. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  14. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  15. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  16. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  17. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  18. def productIterator: Iterator[Any]
    Definition Classes
    Product
  19. def productPrefix: String
    Definition Classes
    Product
  20. final def rotateAccum[E](_right: AndThen[R, E]): AndThen[T, E]
    Attributes
    protected
  21. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  22. def toString(): String
    Definition Classes
    AndThen → Function1 → AnyRef → Any
  23. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  24. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from (T) ⇒ R

Inherited from AnyRef

Inherited from Any

Ungrouped