2019-05-18 23:56:48 -04:00
|
|
|
module Util where
|
|
|
|
|
2019-05-20 19:37:03 -04:00
|
|
|
elemSnd :: (Eq a) => a -> [(a,b)] -> Bool
|
2019-05-20 06:51:06 -04:00
|
|
|
elemSnd a l = any (\(x,_) -> x == a) l
|
|
|
|
|
2019-05-20 19:37:03 -04:00
|
|
|
matchSnd :: (Eq a) => a -> (a,b) -> Bool
|
2019-05-20 06:51:06 -04:00
|
|
|
matchSnd t (a,_) = a == t
|
|
|
|
|
2019-05-20 19:37:03 -04:00
|
|
|
getFirstMatchNS :: [a] -> (a -> Bool) -> a
|
|
|
|
getFirstMatchNS l f = unwrapMaybe (getFirstMatch l f)
|
2019-05-20 06:51:06 -04:00
|
|
|
|
|
|
|
unwrapMaybe :: Maybe a -> a
|
|
|
|
unwrapMaybe (Just a) = a
|
|
|
|
unwrapMaybe _ = error "Brutal Present: Unwrapped Nothing"
|
|
|
|
|
2019-05-18 23:56:48 -04:00
|
|
|
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))
|
|
|
|
|
2019-05-19 00:55:53 -04:00
|
|
|
-- Return the first item that f returns true for
|
2019-05-19 02:11:40 -04:00
|
|
|
getFirstMatch :: [a] -> (a -> Bool) -> Maybe a
|
|
|
|
getFirstMatch [] _ = Nothing
|
2019-05-19 00:55:53 -04:00
|
|
|
getFirstMatch (l:ls) f
|
2019-05-19 02:11:40 -04:00
|
|
|
| f l = Just l
|
2019-05-19 00:55:53 -04:00
|
|
|
| otherwise = getFirstMatch ls f
|
2019-05-19 02:11:40 -04:00
|
|
|
|