Hi there. A question regarding `ior()` builder usa...
# arrow
a
Hi there. A question regarding
ior()
builder usage. I have two functions:
important(): Either<Error, Something>
and
notSoImportant(): Either<Error, Unit>
and want to call them inside a
load(): Ior<Error, Something>
, that would return
Ior.Left
when
important()
fails, and
Both
if only
notSoImportant()
fails. The only way I found to make this work is:
Copy code
ior({ error, _ -> error }) {
    val result = important().bind()

    notSoImportant()
        .fold(
            { Ior.Both(it, Unit) },
            { Ior.Right(it) }
        )
        .bind()

    result
}
This feels a bit too verbose. Is there a better, more convenient way of doing this?
a
I guess you could use
recover
on the second case, along the lines of
Copy code
recover({ notSoImportant().bind() }) { it -> Ior.Both(it, Unit).bind() }
1
the idea is that if it fails, you use
Both
instead
1
a
thx, that looks better
a
actually, I forgot about a few functions that are available on
Ior
blocks, so you can do directly
Copy code
ior({ error, _ -> error }) {
    val result = important().bind()
    notSoImportant().getOrAccumulate() { Unit }
    result
}
a
That is what I was looking for 🙂 One note however that this was added recently, and looks like it didn't make it to 1.2.4 release.
😢 1