You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

114 lines
2.5 KiB

  1. {-# OPTIONS_HADDOCK ignore-exports #-}
  2. module MyPrelude where
  3. import Control.Arrow ((***))
  4. import Data.List
  5. -- |Used to create a common interface for default settings of data types.
  6. class Def a where
  7. def :: a
  8. -- |For negating random types.
  9. class Not b where
  10. not' :: b -> b
  11. -- |Split an array into subarrays depending on a given condition.
  12. splitBy :: (a -> Bool) -- ^ condition
  13. -> [a] -- ^ array to split
  14. -> [[a]] -- ^ splitted array
  15. splitBy f s =
  16. case dropWhile f s of
  17. [] -> []
  18. s' -> w : splitBy f s''
  19. where (w, s'') = break f s'
  20. -- |Remove a given item from a list.
  21. removeItem :: (Eq a) => a -> [a] -> [a]
  22. removeItem x = foldr (\x' y -> if x' == x then y else x':y) []
  23. -- |Sort a list of tuples lexicographically.
  24. sortLex :: (Ord a) => [(a, a)] -> [(a, a)]
  25. sortLex =
  26. sortBy (\(x1, y1) (x2, y2) -> case compare x1 x2 of
  27. EQ -> compare y1 y2
  28. x -> x)
  29. -- |Sort a list of tuples lexicographically, but start with second elem.
  30. sortLexSwapped :: (Ord a) => [(a, a)] -> [(a, a)]
  31. sortLexSwapped =
  32. sortBy (\(x1, y1) (x2, y2) -> case compare y1 y2 of
  33. EQ -> compare x1 x2
  34. y -> y)
  35. -- |Get the last x elements of a list.
  36. getLastX :: Int -> [a] -> [a]
  37. getLastX a xs
  38. | length xs < a = []
  39. | otherwise = snd . splitAt (length xs - a) $ xs
  40. -- |Get a list with it's head and last element cut. If there are less
  41. -- than 3 elements in the list, return an empty list.
  42. tailInit :: [a] -> [a]
  43. tailInit xs
  44. | length xs > 2 = tail . init $ xs
  45. | otherwise = []
  46. -- |Remove duplicates from a list.
  47. rmdups :: (Ord a) => [a] -> [a]
  48. rmdups =
  49. foldl (\seen x -> if x `elem` seen
  50. then seen
  51. else seen ++ [x]) []
  52. -- |Apply a function to the first element of a tuple.
  53. first :: (a -> b) -> (a,c) -> (b,c)
  54. first = (*** id)
  55. -- |Sequentialize a list, such as:
  56. -- [1, 2, 3, 4, 5] -> [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]]
  57. seqList :: [a] -> [[a]]
  58. seqList = tail. inits
  59. -- |Duplicate the last element of a list and append it.
  60. dupLast :: [a] -> [a]
  61. dupLast [] = []
  62. dupLast xs = xs ++ [last xs]
  63. -- |Simpler version of if-then-else.
  64. if' :: Bool -> a -> a -> a
  65. if' True x _ = x
  66. if' False _ y = y
  67. -- |Shift a list k places.
  68. shiftM :: Int -> [a] -> [a]
  69. shiftM _ [] = []
  70. shiftM 0 xs = xs
  71. shiftM n xs = drop n xs ++ take n xs
  72. -- |Shift a list.
  73. -- E.g.: shiftM' (== 5)
  74. shiftM' :: Eq a => a -> [a] -> [a]
  75. shiftM' s xs = dropWhile (/= s) xs ++ takeWhile (/= s) xs
  76. -- |Get the pivot of a list.
  77. pivot :: [a] -> Maybe a
  78. pivot [] = Nothing
  79. pivot xs = Just . (!!) xs . flip div 2 . length $ xs