mapOrAccumulate

inline fun <Error, A, B> Iterable<A>.mapOrAccumulate(combine: (Error, Error) -> Error, transform: RaiseAccumulate<Error>.(A) -> B): Either<Error, List<B>>(source)

Returns Either a List containing the results of applying the given transform function to each element in the original collection, or accumulate all the logical errors that were raised while transforming the collection using the combine function is used to accumulate all the logical errors.

Within this DSL you can bind both Either, and EitherNel values and invoke Raise based function of logical error type Error. Let's see an example of all the different cases:

fun test() {
listOf(1, 2, 3, 4).mapOrAccumulate({ a, b -> "$a, $b" }) { i ->
when(i) {
1 -> "Either - $i".left().bind()
2 -> "EitherNel - $i".leftNel().bindNel()
3 -> raise("Raise - $i")
else -> withNel { raise(nonEmptyListOf("RaiseNel - $i")) }
}
} shouldBe "Either - 1, EitherNel - 2, Raise - 3, RaiseNel - 4".left()
}

Returns Either a List containing the results of applying the given transform function to each element in the original collection, or accumulate all the logical errors into a NonEmptyList that were raised while applying the transform function.

Let's see an example of all the different cases:

fun test() {
listOf(1, 2, 3, 4).mapOrAccumulate { i ->
when(i) {
1 -> "Either - $i".left().bind()
2 -> "EitherNel - $i".leftNel().bindNel()
3 -> raise("Raise - $i")
else -> withNel { raise(nonEmptyListOf("RaiseNel - $i")) }
}
} shouldBe nonEmptyListOf("Either - 1", "EitherNel - 2", "Raise - 3", "RaiseNel - 4").left()
}

inline fun <E, A, B> NonEmptyList<A>.mapOrAccumulate(combine: (E, E) -> E, transform: RaiseAccumulate<E>.(A) -> B): Either<E, NonEmptyList<B>>(source)
fun <Error, A, B> Sequence<A>.mapOrAccumulate(combine: (Error, Error) -> Error, transform: RaiseAccumulate<Error>.(A) -> B): Either<Error, List<B>>(source)
inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(combine: (E, E) -> E, transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B): Either<E, Map<K, B>>(source)
inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B): Either<NonEmptyList<E>, Map<K, B>>(source)