Shabinder Singh
10/19/2022, 6:46 PMList<Either<A,B>>
TO
Either<List<A>,List<B>>
OR
ValidaterNel<A,List<B>>
basically it iterates over either and collect both errors and success,
• .sequence
shortCircuits which isnt wanted here*,*
• .traverse {}
is there but I thought there would a extension function for above as its a common useCase I guess, is there an extension for above ?Shabinder Singh
10/19/2022, 7:00 PMfun <A, B> Iterable<Either<A, B>>.toValidatedNel(): ValidatedNel<A, List<B>> {
return traverse(semigroup = Semigroup.nonEmptyList()) { it.toValidatedNel() }
}
simon.vergauwen
10/19/2022, 7:20 PMEither
and Validated
in Arrow 2.x.x, so this would be covered by that.raulraja
10/19/2022, 8:32 PMShabinder Singh
10/24/2022, 7:19 PMsimon.vergauwen
10/31/2022, 3:37 PMacc = Valid(emptyList())
and then it runs f
with the first element of the Iterable
. If val res = f(a)
returns Invalid
it will check the state of acc
and combine the results when needed.
So for example listOf(1).traverse { Invalid(it) }
it will start with acc = Valid(emptyList<B>())
and then run val res = Invalid(1)
.
Since res
is Invalid
and acc = Valid
it will return the Invalid
case.
Here are a property based test that verify this, https://github.com/arrow-kt/arrow/blob/96c774a08b1c86f369f075b4b8dd81a8904121d1/ar[…]ore/arrow-core/src/commonTest/kotlin/arrow/core/IterableTest.kt