module DDC.Data.ListUtils
( takeHead
, takeTail
, takeInit
, takeMaximum
, index
, findDuplicates)
where
import qualified Data.Set as Set
takeHead :: [a] -> Maybe a
takeHead xs
= case xs of
[] -> Nothing
_ -> Just (head xs)
takeTail :: [a] -> Maybe [a]
takeTail xs
= case xs of
[] -> Nothing
_ -> Just (tail xs)
takeInit :: [a] -> Maybe [a]
takeInit xs
= case xs of
[] -> Nothing
_ -> Just (init xs)
takeMaximum :: Ord a => [a] -> Maybe a
takeMaximum xs
= case xs of
[] -> Nothing
_ -> Just (maximum xs)
index :: [a] -> Int -> Maybe a
index [] _ = Nothing
index (x : _) 0 = Just x
index (_ : xs) i = index xs (i 1)
findDuplicates :: Ord n => [n] -> [n]
findDuplicates xx
= go (Set.fromList xx) xx
where go _ [] = []
go ss (x : xs)
| Set.member x ss = go (Set.delete x ss) xs
| otherwise = x : go ss xs