From 8d89a8873506718baa659db12cbf1be5aea7a9f1 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Thu, 13 Dec 2007 23:51:11 +0000 Subject: [PATCH] Added a couple more functions to the Utils module --- common/Utils.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/common/Utils.hs b/common/Utils.hs index 67de22d..90099bb 100644 --- a/common/Utils.hs +++ b/common/Utils.hs @@ -64,6 +64,12 @@ transformEither funcLeft funcRight x = case x of Left l -> Left (funcLeft l) Right r -> Right (funcRight r) +-- | Splits a list of Either values into two lists (the list of Lefts and the list of Rights) +splitEither :: [Either a b] -> ([a],[b]) +splitEither [] = ([],[]) +splitEither ((Left x):es) = let (ls,rs) = splitEither es in (x:ls,rs) +splitEither ((Right y):es) = let (ls,rs) = splitEither es in (ls,y:rs) + -- | Transforms between two 'Maybe' types using a function: transformMaybe :: (a -> b) -> Maybe a -> Maybe b transformMaybe _ Nothing = Nothing @@ -135,6 +141,14 @@ seqPair (x,y) = do x' <- x y' <- y return (x',y') +-- | Finds the first element that matches the given predicate, and returns +-- (Just) it alongside the other elements of the list if it is found. If no matching +-- element is found, Nothing is returned with the original list +findAndRemove :: (a -> Bool) -> [a] -> (Maybe a, [a]) +findAndRemove _ [] = (Nothing,[]) +findAndRemove f (x:xs) | f x = (Just x,xs) + | otherwise = let (r,xs') = findAndRemove f xs in (r,x:xs') + -- | Forms the powerset of a given list. -- It uses the list monad cleverly, and it scares me. But it works. -- Taken from: http:\/\/www.haskell.org\/haskellwiki\/Blow_your_mind