module Extra.Either where
lefts :: [Either a b] -> [a]
lefts xs = fst (partitionEithers xs)
rights :: [Either a b] -> [b]
rights xs = snd (partitionEithers xs)
isRight (Right _) = True
isRight (Left _) = False
isLeft = not . isRight
concatEithers :: [Either a b] -> Either [a] [b]
concatEithers xs =
case partitionEithers xs of
([], rs) -> Right rs
(ls, _) -> Left ls
partitionEithers :: [Either a b] -> ([a], [b])
partitionEithers xs =
part ([], []) (reverse xs)
where
part (ls, rs) [] = (ls, rs)
part (ls, rs) (Left l : more) = part (l : ls, rs) more
part (ls, rs) (Right r : more) = part (ls, r : rs) more
rightOnly = rights
eitherFromList = concatEithers