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) }
- Alphabetic
- By Inheritance
- AndThen
- Serializable
- Product
- Equals
- Function1
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Abstract Value Members
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
andThen[A](g: (R) ⇒ A): AndThen[T, A]
- Definition Classes
- AndThen → Function1
-
final
def
apply(a: T): R
- Definition Classes
- AndThen → Function1
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
compose[A](g: (A) ⇒ T): AndThen[A, R]
- Definition Classes
- AndThen → Function1
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
productIterator: Iterator[Any]
- Definition Classes
- Product
-
def
productPrefix: String
- Definition Classes
- Product
-
final
def
rotateAccum[E](_right: AndThen[R, E]): AndThen[T, E]
- Attributes
- protected
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AndThen → Function1 → AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()