arrow-fx / arrow.fx / IO / tailRecM

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>) {
  //sampleStart
  val result = IO.tailRecM(0) { i ->
    IO.just(
     if(i == 5000) Right(i)
     else Left(i + 1)
    )
  }
  //sampleEnd
  println(result.unsafeRunSync())
}

Do you like Arrow?

Arrow Org
<