Hi! I don't know yet the abstractions in arrow to ...
# arrow
k
Hi! I don't know yet the abstractions in arrow to achieve what I'm trying to do. Basically, I'd like to do some switchMapping. Here's a very simple example of what I'm trying to do:
Copy code
fun <T, E> IO<T>.setState(handleError: (Throwable) -> E, onSuccess: (T) -> IO<Any>) = IO.fx {
        val state = !this@setState.attempt()
        state.fold(
            { handleError(it) },
            {
                // Other stuff happening…
                !onSuccess(it)
            }
        )
    }

    @Test
    fun test() {
        val handleError = { t: Throwable -> println("Error: ${t.message}") }
        val onSuccess = { bool: Boolean ->
            IO.fx {
                println("Starting")
                !effect { delay(5000) } // --> SwitchMapping should happen around here
                println("Finished with value $bool")
            }
        }
        // Here, this is what I don't know how to "Observablify" to send multiple events
        IO.just(true).setState(handleError, onSuccess).unsafeRunAsync {}
        IO.just(false).setState(handleError, onSuccess).unsafeRunSync()
    }
What I expect is:
Copy code
Starting
Starting
Finished with value false // The other one should be "switchMapped"
I don't know how to have some kind of flow or observable with arrow, and I don't know how to switch map within it. Any guidance?
p
IO
is for single results, so flatMap is switchMap
if I( understand the question correctly
k
Actually, I was looking for the multiple-result version of IO (or the arrow equivalent of a Flow maybe ?), and therefore the corresponding switchMap.
p
cool
so, multi-result for single consumer is called Queue
because it's single-consumer it works with
concatMap
semantics by default
you can build
switchMap
on top of it
Copy code
EDIT OUT
something like that
nevermind, that won't work
hmmm
it's feasible, just a bit complicated to implement
I'll see if I can do it later today
👍 1