{-# LANGUAGE Haskell2010 #-}
{-# OPTIONS -Wall #-}

module Haskell.X where

import Data.List
import Data.Ord
import Control.Arrow



-- | Apply a function exhaustively.
exhaustively :: Eq a => (a -> a) -> a -> a
exhaustively = exhaustivelyBy (==)

-- | Apply a function exhaustively.
exhaustivelyBy :: (a -> a -> Bool) -> (a -> a) -> a -> a
exhaustivelyBy predicate func dat = case predicate dat result of
    True -> result
    False -> exhaustivelyBy predicate func result
  where result = func dat

-- | Apply a monad function exhaustively.
exhaustivelyM :: (Eq a, Monad m) => (a -> m a) -> a -> m a
exhaustivelyM = exhaustivelyByM (==)

-- | Apply a monad function exhaustively.
exhaustivelyByM :: Monad m => (a -> a -> Bool) -> (a -> m a) -> a -> m a
exhaustivelyByM predicate func dat = do
    result <- func dat
    case predicate dat result of
        True -> return result
        False -> exhaustivelyByM predicate func result

-- | Sort a list and leave out duplicates. Like @nub . sort@ but faster.
uniqSort :: (Ord a) => [a] -> [a]
uniqSort = map head . group . sort

-- | Sort, then group
aggregateBy :: (a -> a -> Ordering) -> [a] -> [[a]]
aggregateBy x = groupBy (\a b -> x a b == EQ) . sortBy x

-- | Sort, then group
aggregate :: (Ord a) => [a] -> [[a]]
aggregate = aggregateBy compare

-- | Aggregate an association list, such that keys become unique.
--
-- (c) 
aggregateAL :: (Ord a) => [(a,b)] -> [(a,[b])]
aggregateAL = map (fst . head &&& map snd) . aggregateBy (comparing fst)

-- | Replace all occurences of a specific thing in a list of things another thing. 
tr :: Eq a => a -> a -> [a] -> [a]
tr n r (x:xs)
    | x == n = r : tr n r xs
    | otherwise = x : tr n r xs
tr _ _ [] = []