2014-10-07 17:22:37 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
2014-10-10 15:40:08 +00:00
|
|
|
module MyPrelude where
|
|
|
|
|
2014-12-14 17:01:13 +00:00
|
|
|
import Control.Arrow ((***))
|
2014-10-11 23:54:44 +00:00
|
|
|
import Data.List
|
|
|
|
|
2014-10-10 15:40:08 +00:00
|
|
|
|
|
|
|
-- |Used to create a common interface for default settings of data types.
|
|
|
|
class Def a where
|
|
|
|
def :: a
|
2014-10-07 17:22:37 +00:00
|
|
|
|
|
|
|
|
2014-11-29 17:06:07 +00:00
|
|
|
-- |For negating random types.
|
|
|
|
class Not b where
|
|
|
|
not' :: b -> b
|
|
|
|
|
|
|
|
|
2014-10-07 17:22:37 +00:00
|
|
|
-- |Split an array into subarrays depending on a given condition.
|
|
|
|
splitBy :: (a -> Bool) -- ^ condition
|
|
|
|
-> [a] -- ^ array to split
|
|
|
|
-> [[a]] -- ^ splitted array
|
2014-10-09 22:19:05 +00:00
|
|
|
splitBy f s =
|
|
|
|
case dropWhile f s of
|
|
|
|
[] -> []
|
|
|
|
s' -> w : splitBy f s''
|
|
|
|
where (w, s'') = break f s'
|
2014-10-08 14:27:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Remove a given item from a list.
|
|
|
|
removeItem :: (Eq a) => a -> [a] -> [a]
|
|
|
|
removeItem x = foldr (\x' y -> if x' == x then y else x':y) []
|
2014-10-11 23:54:44 +00:00
|
|
|
|
|
|
|
|
2015-01-08 00:38:46 +00:00
|
|
|
-- |Sort a list of tuples lexicographically.
|
2014-10-11 23:54:44 +00:00
|
|
|
sortLex :: (Ord a) => [(a, a)] -> [(a, a)]
|
|
|
|
sortLex =
|
|
|
|
sortBy (\(x1, y1) (x2, y2) -> case compare x1 x2 of
|
|
|
|
EQ -> compare y1 y2
|
|
|
|
x -> x)
|
|
|
|
|
|
|
|
|
2015-01-08 00:38:32 +00:00
|
|
|
-- |Sort a list of tuples lexicographically, but start with second elem.
|
|
|
|
sortLexSwapped :: (Ord a) => [(a, a)] -> [(a, a)]
|
|
|
|
sortLexSwapped =
|
|
|
|
sortBy (\(x1, y1) (x2, y2) -> case compare y1 y2 of
|
|
|
|
EQ -> compare x1 x2
|
|
|
|
y -> y)
|
|
|
|
|
|
|
|
|
2014-10-12 01:00:55 +00:00
|
|
|
-- |Get the last x elements of a list.
|
|
|
|
getLastX :: Int -> [a] -> [a]
|
|
|
|
getLastX a xs
|
|
|
|
| length xs < a = []
|
|
|
|
| otherwise = snd . splitAt (length xs - a) $ xs
|
|
|
|
|
|
|
|
|
2014-10-11 23:54:44 +00:00
|
|
|
-- |Get a list with it's head and last element cut. If there are less
|
2014-12-14 17:01:42 +00:00
|
|
|
-- than 3 elements in the list, return an empty list.
|
2014-10-11 23:54:44 +00:00
|
|
|
tailInit :: [a] -> [a]
|
|
|
|
tailInit xs
|
|
|
|
| length xs > 2 = tail . init $ xs
|
|
|
|
| otherwise = []
|
2014-10-11 23:57:20 +00:00
|
|
|
|
|
|
|
|
2014-10-12 01:00:13 +00:00
|
|
|
-- |Remove duplicates from a list.
|
|
|
|
rmdups :: (Ord a) => [a] -> [a]
|
|
|
|
rmdups =
|
|
|
|
foldl (\seen x -> if x `elem` seen
|
|
|
|
then seen
|
|
|
|
else seen ++ [x]) []
|
|
|
|
|
|
|
|
|
2014-10-11 23:57:20 +00:00
|
|
|
-- |Apply a function to the first element of a tuple.
|
|
|
|
first :: (a -> b) -> (a,c) -> (b,c)
|
2014-12-14 17:01:13 +00:00
|
|
|
first = (*** id)
|
2014-10-12 00:31:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Sequentialize a list, such as:
|
|
|
|
-- [1, 2, 3, 4, 5] -> [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]]
|
|
|
|
seqList :: [a] -> [[a]]
|
2014-10-12 14:09:17 +00:00
|
|
|
seqList = tail. inits
|
2014-10-12 00:57:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Duplicate the last element of a list and append it.
|
|
|
|
dupLast :: [a] -> [a]
|
|
|
|
dupLast [] = []
|
|
|
|
dupLast xs = xs ++ [last xs]
|
2014-10-13 20:30:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Simpler version of if-then-else.
|
|
|
|
if' :: Bool -> a -> a -> a
|
|
|
|
if' True x _ = x
|
|
|
|
if' False _ y = y
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Shift a list k places.
|
|
|
|
shiftM :: Int -> [a] -> [a]
|
|
|
|
shiftM _ [] = []
|
|
|
|
shiftM 0 xs = xs
|
|
|
|
shiftM n xs = drop n xs ++ take n xs
|
2014-11-29 04:09:58 +00:00
|
|
|
|
|
|
|
|
2015-01-08 00:38:18 +00:00
|
|
|
-- |Shift a list.
|
|
|
|
-- E.g.: shiftM' (== 5)
|
|
|
|
shiftM' :: Eq a => a -> [a] -> [a]
|
|
|
|
shiftM' s xs = dropWhile (/= s) xs ++ takeWhile (/= s) xs
|
|
|
|
|
|
|
|
|
2014-11-29 04:09:58 +00:00
|
|
|
-- |Get the pivot of a list.
|
|
|
|
pivot :: [a] -> Maybe a
|
|
|
|
pivot [] = Nothing
|
|
|
|
pivot xs = Just . (!!) xs . flip div 2 . length $ xs
|