evac/Util.hs

44 lines
1.1 KiB
Haskell

module Util where
elemSnd :: a -> [(a,b)] -> Bool
elemSnd a l = any (\(x,_) -> x == a) l
matchSnd :: a -> (a,b) -> Bool
matchSnd t (a,_) = a == t
getFirstMatchNS :: [a] -> (a -> Bool) -> [a]
getFirstMatchNS l f = unwrapMaybe . (getFirstMatch l f)
unwrapMaybe :: Maybe a -> a
unwrapMaybe (Just a) = a
unwrapMaybe _ = error "Brutal Present: Unwrapped Nothing"
rmFirstMatch :: [a] -> (a -> Bool) -> [a]
rmFirstMatch [] _ = []
rmFirstMatch (l:ls) f
| f l = ls
| otherwise = (l : rmFirstMatch ls f)
unwrapLeft :: Either a b -> a
unwrapLeft (Left x) = x
unwrapLeft (Right _) = error "Not a Left value"
unwrapRight :: Either a b -> b
unwrapRight (Right x) = x
unwrapRight (Left _) = error "Not a Right value"
-- Remove duplicates, stabily
uniq :: (Eq a) => [a] -> [a]
uniq x = reverse $ go x []
where
go [] al = al
go (c:cs) al = if (c `elem` al) then (go cs al) else (go cs (c:al))
-- Return the first item that f returns true for
getFirstMatch :: [a] -> (a -> Bool) -> Maybe a
getFirstMatch [] _ = Nothing
getFirstMatch (l:ls) f
| f l = Just l
| otherwise = getFirstMatch ls f