Andrew
06/29/2024, 2:24 AMior()
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:
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?Alejandro Serrano.Mena
06/29/2024, 7:23 PMrecover
on the second case, along the lines of
recover({ notSoImportant().bind() }) { it -> Ior.Both(it, Unit).bind() }
Alejandro Serrano.Mena
06/29/2024, 7:23 PMBoth
insteadAndrew
06/30/2024, 2:34 AMAlejandro Serrano.Mena
07/01/2024, 7:21 AMIor
blocks, so you can do directly
ior({ error, _ -> error }) {
val result = important().bind()
notSoImportant().getOrAccumulate() { Unit }
result
}
Andrew
07/01/2024, 1:59 PM