A function from [Either a b] to Either [a] [b]

This is (almost) sequence for Validation; you just need to convert your as to [a]s. So:

traverse (eitherToValidation . first pure)

If you rewrite your producer to produce Validations instead of Eithers in the first place it will be even less noisy.


Well certainly you can get a single Left or the Right using the either monad:

Prelude> let a = [Right "hello", Right "World", Right "Bye"]
Prelude> sequence a
Right ["hello","World","Bye"]
Prelude> let a = [Right "hello", Right "World", Left "Bye"]
Prelude> sequence a
Left "Bye"

But to get all the Left values doesn't seem like anything in the base or common libraries I know so you're probably left rolling your own solution. That said, I think we can find a much more readable solution. I propose:

f x | any isLeft x = lefts x
    | otherwise    = rights x

or

f x | not (null ls) = ls
    | otherwise     = rs
 where (ls,rs) = partitionEithers x

Tags:

Haskell