I'm looking for a cleaner way on how to create a conditional flatMap()
, I've read this but I'm having trouble applying it on my code:
// given variables for the sake of simplicity
val stringSingle = Single.just("dog")
val isCatEat = Single.just(true)
val feedCat = Single.just(true)
// example
stringSingle
.flatMap { string ->
if (string == "cat") {
return@flatMap isCatEat()
.flatMap { isCatEat ->
if (isCatEat) { // if cat already ate, proceed immediately
Single.fromCallable { true }
} else { // if not, feed cat
feedCat()
}
}
} else {
Single.fromCallable { false }
}
}
as you can see (well, the code is very ugly, nesting ugh), I want to avoid calling the feedCat()
by checking it first if the cat already ate. I'm having trouble applying compose()
function as I can't reproduce my condition.