summaryrefslogtreecommitdiff
path: root/Util.hs
blob: 9c3fbf07c7117bb7a062bca0493824bc324ed0ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{-# LANGUAGE TupleSections #-}
module Util where

import Data.Function (on)
import Data.List
import qualified Data.Map.Strict as Map


uniq :: Eq a => [a] -> [a]
uniq (x:y:zs) | x == y = uniq (y:zs)
              | otherwise = x : uniq (y:zs)
uniq l = l

sortUniq :: Ord a => [a] -> [a]
sortUniq = uniq . sort

oppositeGraph :: (Show a, Ord a) => Map.Map a [a] -> Map.Map a [a]
oppositeGraph graph =
    let nodes = concat [k : vs | (k, vs) <- Map.assocs graph]
        edges = map ((,) <$> fst . head <*> map snd)
                . groupBy ((==) `on` fst)
                . sortOn fst
                $ [(to, from) | (from, tos) <- Map.assocs graph, to <- tos]
    in Map.fromList (map (,[]) nodes ++ edges)