module Tip.Utils where
import Data.List
import Data.Graph
import Data.List.Split
import Data.Char
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import Data.Ord
usort :: Ord a => [a] -> [a]
usort = map head . group . sort
sortThings :: Ord name => (thing -> name) -> (thing -> [name]) -> [thing] -> [[thing]]
sortThings name refers things =
map flattenSCC $ stronglyConnComp
[ (thing,name thing,filter (`elem` names) (refers thing))
| thing <- things
]
where
names = map name things
flagify :: String -> String
flagify
= map toLower . intercalate "-"
. split (condense $ dropInitBlank $ keepDelimsL $ whenElt (\x -> isUpper x || isSpace x))
flagifyShow :: Show a => a -> String
flagifyShow = flagify . show
maximumOn :: forall f a b . (F.Foldable f,Ord b) => (a -> b) -> f a -> b
maximumOn f = f . F.maximumBy (comparing f)