I'm experimenting with Arrow's `raise()` functiona...
# functional
n
I'm experimenting with Arrow's
raise()
functionality and I try to find a library function similar to the following hypothetical
orElse()
:
Copy code
context(Raise<String>)
fun f1() = 1

context(Raise<String>)
fun f2(): Nothing = raise("error")

suspend inline fun <R, A> Effect<R, A>.orElse(noinline block: suspend (R) -> A) =
    fold(
        recover = block,
        transform = ::identity
    )

val v1 = effect { f1() }.orElse { 33 }
assertEquals(1, v1)

val v2 = effect { f2() }.orElse { 33 }
assertEquals(33, v2)
I use arrow 1.1.6-alpha.36. Thanks.
s
Such an API doesn't exist yet 🤔 but would be welcomed! I think the signature should probably be
getOrElse(onRaise: (R) -> A): A
. To be in line with other operators and Kotlin Std. A PR would be awesome, or a issue 🙏
You can do
either { f1() }.getOrElse { 33 }
though.
That would work in your case here without ever even needing
suspend
n
without ever even needing
suspend
Really, thanks. (TBH I'm a little bit confused when I look at the sources because Idea often takes me to the wrong source codes. When I start at browsing
Raise<R>
I often end up at functions related to
arrow.core.continuations.Effect
for some reason 😕 )
s
That is really strange.. I have some issues with browsing code in IDEA. I can almost never see code from dependencies 😕 I know some issues have been reported about this but not sure where it stands.
n
425 Views