Masrur Mirboboev
09/05/2022, 8:15 PMtraverse
seems to short circuit on the first failure.Masrur Mirboboev
09/05/2022, 8:17 PMfun <A, B> Iterable<B>.traverseAll(f: (B) -> Either<A, B>): Either<List<A>, List<B>> {
val lefts: MutableList<A> = mutableListOf()
val rights: MutableList<B> = mutableListOf()
forEach { f(it).fold(lefts::add, rights::add) }
return conditionally(
test = lefts.isEmpty(),
ifTrue = { rights },
ifFalse = { lefts }
)
}
qohatpp
09/05/2022, 9:22 PMList<B>
and one function of type (B) -> Either<A, B>
and you want to build Either<A, List<B>>
accumulating the errors, You should think about Semigroup, So I think you can take a look at traverse(Semigroup, B -> Validated<A, B>) or parTraverseValidated()
, Here a really good example https://www.47deg.com/blog/functional-domain-modeling-part-2simon.vergauwen
09/06/2022, 7:07 AMValidated<NonEmptyList<E>, A>
and it will return Validated<NonEmptyList<E>, List<A>>
from traverse
and there will be no need to pass Semigroup
.
It uses Semigroup
for NonEmptyList
by default which is { listA, listB -> listA + listB }
so concatenation of the errors lists.Masrur Mirboboev
09/07/2022, 8:04 AMstojan
09/08/2022, 11:00 AMsuspend
for functions with side effects.... so you can use Validated + suspend functions