genovich
02/21/2021, 8:35 PMraulraja
02/21/2021, 11:38 PMsimon.vergauwen
02/22/2021, 8:11 AMIO you can simply do something like:
fun IO<Int>.loop(n: Int): IO<Int> =
flatMap { i ->
if(i == n) this
else IO { i + 1 }.loop( n)
}
IO.just(0).loop(10000)
And you could do the same with tailRecM.
val n = 10_000
IO.tailRecM(0) { i ->
IO {
if(i == n) Right(i)
else Left(i + 1)
}
}
Where with tailRecM we signal using Either if we should continue or not, where Right signals we found a final result and don't need to keep recursing.
This is all gone with Arrow Fx Coroutines though since with suspend we can simply write tailrec functions. So this the translation of the first IO example to a suspend one.
suspend tailrec fun loop(prev: Int, n: Int): Int =
if(prev == n) prev
else loop(prev + 1, n)
prev(0, 10000)simon.vergauwen
02/22/2021, 8:16 AMSchedule unless you introduce some atomic field.
val n = 10_000
val count = Atomic(0)
arrow.fx.coroutines.repeat(
Schedule.forever<Int>() zipRight Schedule.doUntil { i -> i == n }
) {
count.updateAndGet(Int::inc)
}genovich
02/22/2021, 8:22 AMprev a part of IO? I don't see that field earlier.simon.vergauwen
02/22/2021, 8:23 AMgenovich
02/25/2021, 3:57 PMsimon.vergauwen
02/25/2021, 4:57 PMObservable from a Schedule or do you want to repeat/retry an Observable based on a Schedule?genovich
02/26/2021, 6:42 AMIO and Single, then I want to implement something like Single.tailRec()simon.vergauwen
02/26/2021, 7:57 AMfun <A, B> tailRecM(a: A, f: (A) -> Single<Either<A, B>>): Single<B> =
f(a).flatMap {
when (it) {
is Either.Left -> tailRecM(it.a, f)
is Either.Right -> Single.just(it.b)
}
}genovich
02/26/2021, 11:24 AMgenovich
02/26/2021, 11:27 AMf(a).subscribeOn(Schedulers.computation()).flatMap() then it hangs with about 20000 steps, but whole work were done.simon.vergauwen
02/26/2021, 12:41 PMflatMap operator..simon.vergauwen
02/26/2021, 12:42 PMsimon.vergauwen
02/26/2021, 12:46 PMtailRecMgenovich
02/27/2021, 2:00 PM(T) -> SourceOf<V>, where SourceOf<V> is like IO or Single. In this case if I want to represent something like EditText, I need to do something like fun loop(initial: String, view: (String) -> SourceOf<String>, endPredicate: (String) -> Boolean): SourceOf<String>simon.vergauwen
03/01/2021, 7:37 AMgenovich
03/01/2021, 9:34 AMsimon.vergauwen
03/01/2021, 10:41 AMsimon.vergauwen
03/01/2021, 10:42 AMDeepRecursiveFunction https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deep-recursive-function/genovich
03/01/2021, 11:42 AMsimon.vergauwen
03/01/2021, 2:35 PMgenovich
03/01/2021, 5:02 PMsimon.vergauwen
03/01/2021, 5:06 PMgenovich
03/01/2021, 5:10 PM