arrow-fx / arrow.fx / IO / Companion / tailRecM


fun <A, B> tailRecM(a: A, f: (A) -> IOOf<Either<A, B>>): IO<B>

Perform a recursive operation in a stack-safe way, by checking the inner Either value. If you want to continue the recursive operation return Either.Left with the intermediate result A, Either.Right indicates the terminal event and must thus return the resulting value B.

import arrow.core.*
import arrow.fx.IO

fun main(args: Array<String>) {
  val result = IO.tailRecM(0) { i ->
     if(i == 5000) Right(i)
     else Left(i + 1)

Do you like Arrow?

Arrow Org