AndThen

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).

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)
 }
Companion
object
trait Serializable
trait Product
trait Equals
trait T => R
class Object
trait Matchable
class Any

Value members

Concrete methods

override def andThen[A](g: R => A): AndThen[T, A]
Definition Classes
Function1
final def apply(a: T): R
override def compose[A](g: A => T): AndThen[A, R]
Definition Classes
Function1
override def toString: String
Definition Classes
Function1 -> Any

Inherited methods

def canEqual(that: Any): Boolean
Inherited from
Equals
def productArity: Int
Inherited from
Product
def productElement(n: Int): Any
Inherited from
Product
def productElementName(n: Int): String
Inherited from
Product
def productElementNames: Iterator[String]
Inherited from
Product
def productIterator: Iterator[Any]
Inherited from
Product
def productPrefix: String
Inherited from
Product