I have 2 functions which return Results. `function...
# functional
j
I have 2 functions which return Results.
functionOne: 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 ugly
Copy code
f1().map { something ->
  f2().map { it }
  something
}
will it work?
e
no, result of line 2 is always ignored
Copy code
f1().mapCatching { t ->
      f2().onFailure { throw it}
      t
   }
might be easiest
j
alright, thanks!
e
if you were to write it without
Result
, it would look like
Copy code
run {
    val x = f1()
    f2()
    x
}
(or something simpler like
f1().also { f2() }
)
with
Result
, you just translate that to include monadic bind, e.g. `.getOrThrow()`:
Copy code
runCatching {
    val x = f1().getOrThrow()
    f2().getOrThrow()
    x
}
(or
runCatching { f1().getOrThrow().also { f2().getOrThrow() } }
)
if you were using #arrow's Either instead of kotlin.Result then
Copy code
either {
    val x = f1().bind()
    f2().bind()
    x
}
(or the short version) is the intended usage: https://arrow-kt.io/docs/patterns/monad_comprehensions/
p
one().flatMap { it -> two.map { it } }
which is what
flatTap
does in Arrow