Mervyn McCreight
04/09/2021, 11:05 AMval 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.stojan
04/09/2021, 11:32 AMlist.map function that takes a suspend function as an argument, you can use that onesimon.vergauwen
04/09/2021, 11:34 AMinline 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() }.Mervyn McCreight
04/09/2021, 11:44 AMEither ? 🤔stojan
04/09/2021, 11:50 AMlist.map { Either.catch { suspendFun() } } then use traverseEithersimon.vergauwen
04/09/2021, 2:52 PMlist.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:
list.traverseValidated {
Validated.catchNel {
suspendFun()
}
}