2014-10-07 17:22:37 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
2014-10-10 15:40:08 +00:00
|
|
|
module MyPrelude where
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
-- |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
|
|
|
|
|
|
|
|
|
|
|
-- |Sort a liste of tuples lexicographically.
|
|
|
|
sortLex :: (Ord a) => [(a, a)] -> [(a, a)]
|
|
|
|
sortLex =
|
|
|
|
sortBy (\(x1, y1) (x2, y2) -> case compare x1 x2 of
|
|
|
|
EQ -> compare y1 y2
|
|
|
|
x -> x)
|
|
|
|
|
|
|
|
|
|
|
|
-- |Get a list with it's head and last element cut. If there are less
|
|
|
|
-- than 2 elements in the list, return an empty list.
|
|
|
|
tailInit :: [a] -> [a]
|
|
|
|
tailInit xs
|
|
|
|
| length xs > 2 = tail . init $ xs
|
|
|
|
| otherwise = []
|
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)
|
|
|
|
first f (x,y) = (f x, y)
|