arrow-core-data / arrow.core / AndThen

AndThen

@higherkind sealed class AndThen<A, B> : (A) -> B

AndThen wraps a function of shape (A) -> B and can be used to do function composition. It’s similar to arrow.core.andThen and arrow.core.compose and can be used to build stack safe data structures that make use of lambdas. Usage is typically used for signature such as A -> Kind<F, A> where F has a arrow.typeclasses.Monad instance i.e. StateT.flatMap.

As you can see the usage of AndThen is the same as `arrow.core.andThen except we start our computation by wrapping our function in AndThen.

import arrow.core.andThen
import arrow.core.AndThen
import arrow.core.extensions.list.foldable.foldLeft

fun main(args: Array<String>) {
  //sampleStart
  val f = (0..10000).toList()
    .fold({ x: Int -> x + 1 }) { acc, _ ->
      acc.andThen { it + 1 }
    }

  val f2 = (0..10000).toList()
    .foldLeft(AndThen { x: Int -> x + 1 }) { acc, _ ->
      acc.andThen { it + 1 }
    }
  //sampleEnd
  println("f(0) = ${f(0)}, f2(0) = ${f2(0)}")
}

Functions

andThen Compose a function to be invoked after the current function is invoked.fun <X> andThen(g: (B) -> X): AndThen<A, X>
andThenF fun <X> andThenF(right: AndThen<B, X>): AndThen<A, X>
ap fun <C> ap(ff: <ERROR CLASS><A, (B) -> C>): AndThen<A, C>
compose Compose a function to be invoked before the current function is invoked.infix fun <C> compose(g: (C) -> A): AndThen<C, B>
composeF fun <X> composeF(right: AndThen<X, A>): AndThen<X, B>
contramap Alias for composefun <C> contramap(f: (C) -> A): AndThen<C, B>
flatMap fun <C> flatMap(f: (B) -> <ERROR CLASS><A, C>): AndThen<A, C>
invoke Invoke the [AndThen] functionopen fun invoke(a: A): B
map Alias for andThenfun <C> map(f: (B) -> C): AndThen<A, C>
toString open fun toString(): String

Companion Object Functions

id fun <A> id(): AndThen<A, A>
invoke Wraps a function in AndThen.operator fun <A, B> invoke(f: (A) -> B): AndThen<A, B>
just fun <A, B> just(b: B): AndThen<A, B>
tailRecM fun <I, A, B> tailRecM(a: A, f: (A) -> <ERROR CLASS><I, Either<A, B>>): AndThen<I, B>

Do you like Arrow?

Arrow Org
<