I feel stupid.. is there no way to do `Either.left...
# arrow
e
I feel stupid.. is there no way to do
Either.leftOrNull()
? For instance if I have a
List<Either<A,B>>
and want to map that into a
List<A>
?
Ah, okay.. Partially figured it out. Now just gotta turn it into some nicer code.. 🙂
Copy code
val unpackedProblems = problems.mapNotNull {
      when (it) {
         is Either.Left -> it.value
         else -> null
      }
   }
s
Either<List<A>> would be nicer though.
Then you can take you rrrors all the way to the end
i
Maybe you are looking for an accumulating errors use case? Then you can enter 
ValidatedNel<A, B>
 :
Copy code
val unpackedProblems = problems
 .map { it.toValidatedNel() }
 .sequenceValidated()
 .tapInvalid { //consume your NonEmptyList of A typed errors }
e
Thanks @Ivan Lorenz, that looks a lot more like what I should be doing 🙂
Copy code
results
      .map { it.toValidatedNel() }
      .sequenceValidated()
      .tap { generateResultEmail(it) }
      .tapInvalid { generateProblemEmails(it) }
👌 1