João Gabriel Zó
03/17/2023, 6:00 PMfunctionOne: Result<Something>
functionTwo: Result<Unit>
I’ll only call functionTwo if the first one returns a Success, and return a Result<Something>
in case both of them return a Success.
What’s the best way to do it using std lib? I thought the nested folds and maps and ifs turned out a bit uglyf1().map { something ->
f2().map { it }
something
}
will it work?Emil Kantis
03/17/2023, 6:16 PMf1().mapCatching { t ->
f2().onFailure { throw it}
t
}
might be easiestJoão Gabriel Zó
03/17/2023, 6:30 PMephemient
03/18/2023, 6:09 AMResult
, it would look like
run {
val x = f1()
f2()
x
}
(or something simpler like f1().also { f2() }
)Result
, you just translate that to include monadic bind, e.g. `.getOrThrow()`:
runCatching {
val x = f1().getOrThrow()
f2().getOrThrow()
x
}
(or runCatching { f1().getOrThrow().also { f2().getOrThrow() } }
)either {
val x = f1().bind()
f2().bind()
x
}
(or the short version) is the intended usage: https://arrow-kt.io/docs/patterns/monad_comprehensions/pakoito
03/20/2023, 10:04 AMone().flatMap { it -> two.map { it } }
which is what flatTap
does in Arrow