Add new utility function, partitionWith
authorsimonpj@microsoft.com <unknown>
Fri, 10 Nov 2006 13:08:36 +0000 (13:08 +0000)
committersimonpj@microsoft.com <unknown>
Fri, 10 Nov 2006 13:08:36 +0000 (13:08 +0000)
compiler/utils/Util.lhs

index 3eff34b..a7b65e8 100644 (file)
@@ -12,7 +12,7 @@ module Util (
         zipLazy, stretchZipWith,
        mapFst, mapSnd,
        mapAndUnzip, mapAndUnzip3,
-       nOfThem, filterOut,
+       nOfThem, filterOut, partitionWith,
 
        lengthExceeds, lengthIs, lengthAtLeast, 
        listLengthCmp, atLength, equalLength, compareLength,
@@ -168,6 +168,15 @@ 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
+
 \end{code}
 
 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists