%
+% (c) The University of Glasgow 2006
% (c) The University of Glasgow 1992-2002
%
\section[Util]{Highly random utility functions}
zipLazy, stretchZipWith,
mapFst, mapSnd,
mapAndUnzip, mapAndUnzip3,
- nOfThem, filterOut,
+ nOfThem, filterOut, partitionWith, splitEithers,
lengthExceeds, lengthIs, lengthAtLeast,
listLengthCmp, atLength, equalLength, compareLength,
import Panic ( panic, trace )
import FastTypes
-import EXCEPTION ( Exception(..), finally, throwDyn, catchDyn, throw )
-import qualified EXCEPTION as Exception
-import DYNAMIC ( Typeable )
-import DATA_IOREF ( IORef, newIORef )
-import UNSAFE_IO ( unsafePerformIO )
-import DATA_IOREF ( readIORef, writeIORef )
+import Control.Exception ( Exception(..), finally, catchDyn, throw )
+import qualified Control.Exception as Exception
+import Data.Dynamic ( Typeable )
+import Data.IORef ( IORef, newIORef )
+import System.IO.Unsafe ( unsafePerformIO )
+import Data.IORef ( readIORef, writeIORef )
-import qualified List ( elem, notElem )
+import qualified Data.List as List ( elem, notElem )
#ifndef DEBUG
-import List ( zipWith4 )
+import Data.List ( zipWith4 )
#endif
-import Monad ( when )
-import IO ( catch, isDoesNotExistError )
-import Directory ( doesDirectoryExist, createDirectory )
-import Char ( isUpper, isAlphaNum, isSpace, ord, isDigit )
-import Ratio ( (%) )
-import Time ( ClockTime )
-import Directory ( getModificationTime )
+import Control.Monad ( when )
+import SYSTEM_IO_ERROR as IO ( catch, isDoesNotExistError )
+import System.Directory ( doesDirectoryExist, createDirectory,
+ getModificationTime )
+import Data.Char ( isUpper, isAlphaNum, isSpace, ord, isDigit )
+import Data.Ratio ( (%) )
+import System.Time ( ClockTime )
infixr 9 `thenCmp`
\end{code}
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