From 308cb59e89a5be1ed6d55da39a843ca5f1828445 Mon Sep 17 00:00:00 2001 From: "simonpj@microsoft.com" Date: Fri, 10 Nov 2006 13:08:36 +0000 Subject: [PATCH] Add new utility function, partitionWith --- compiler/utils/Util.lhs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/utils/Util.lhs b/compiler/utils/Util.lhs index 3eff34b..a7b65e8 100644 --- a/compiler/utils/Util.lhs +++ b/compiler/utils/Util.lhs @@ -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 -- 1.7.10.4