Hello :-) What is the new way to do something lik...
# arrow
m
Hello :-) What is the new way to do something like:
Copy code
val someList = ...
val someListOfIOs = someList.map { IO { ... } }
someListOfIOs.sequence(IO.applicativeError())
Now that IO is gone (and sequence is deprecated)? 🤔 Image I have a list of items, and want to do some side-effect actions for each, but want to short-circuit everything when the first fails.
s
AFIK there is a
list.map
function that takes a
suspend
function as an argument, you can use that one
s
inline Iterable<A>.map(..): List<B>
since the definition in the Kotlin Std is
inline
you can simply call
suspend fun
from inside it. So you can simple do
someList.map { suspendfun() }
.
👍 1
m
Thanks, I'll try that one. The short-circuiting here works because of how exceptions work, am I right? How would you solve this if you would not want to raise Exceptions but rather communicate errors using
Either
? 🤔
s
list.map { Either.catch { suspendFun() } }
then use
traverseEither
s
So
list.traverseEither { Either.catch { suspendFun() } }
if you don't want to short-circuit but run all tasks, and collect the
Throwable
into
NonEmptyList
you can also do:
Copy code
list.traverseValidated {
  Validated.catchNel {
    suspendFun()
  }
}