cga/MyPrelude.hs

48 lines
1.1 KiB
Haskell
Raw Normal View History

{-# OPTIONS_HADDOCK ignore-exports #-}
2014-10-10 15:40:08 +00:00
module MyPrelude where
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
-- |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) []
-- |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 = []
-- |Apply a function to the first element of a tuple.
first :: (a -> b) -> (a,c) -> (b,c)
first f (x,y) = (f x, y)