Big tidy-up of deriving code
[ghc-hetmet.git] / compiler / utils / Util.lhs
index 3eff34b..6463c1a 100644 (file)
@@ -12,7 +12,7 @@ module Util (
         zipLazy, stretchZipWith,
        mapFst, mapSnd,
        mapAndUnzip, mapAndUnzip3,
-       nOfThem, filterOut,
+       nOfThem, filterOut, partitionWith, splitEithers,
 
        lengthExceeds, lengthIs, lengthAtLeast, 
        listLengthCmp, atLength, equalLength, compareLength,
@@ -168,6 +168,22 @@ filterOut :: (a->Bool) -> [a] -> [a]
 filterOut p [] = []
 filterOut p (x:xs) | p x       = filterOut p xs
                   | otherwise = x : filterOut p xs
+
+partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
+partitionWith f [] = ([],[])
+partitionWith f (x:xs) = case f x of
+                          Left  b -> (b:bs, cs)
+                          Right c -> (bs, c:cs)
+                      where
+                        (bs,cs) = partitionWith f xs
+
+splitEithers :: [Either a b] -> ([a], [b])
+splitEithers [] = ([],[])
+splitEithers (e : es) = case e of
+                         Left x -> (x:xs, ys)
+                         Right y -> (xs, y:ys)
+                     where
+                       (xs,ys) = splitEithers es
 \end{code}
 
 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists