diff options
author | tomsmeding <tom.smeding@gmail.com> | 2019-12-14 09:53:22 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2019-12-14 09:55:57 +0100 |
commit | c44ae0b58e402ebb3240edf15b5241180cd634fc (patch) | |
tree | be0eec1ba3df49a516b0c1c8d60c8eb6b8b127af /Util.hs | |
parent | 58e8fb619b63b7588fc370b16c0574feee7ab1cb (diff) |
Simplify oppositeGraph
Diffstat (limited to 'Util.hs')
-rw-r--r-- | Util.hs | 9 |
1 files changed, 2 insertions, 7 deletions
@@ -1,7 +1,6 @@ {-# LANGUAGE TupleSections #-} module Util where -import Data.Function (on) import Data.List import qualified Data.Map.Strict as Map @@ -14,11 +13,7 @@ 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 :: 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) + in Map.fromListWith (++) (map (,[]) nodes ++ [(to, [from]) | (from, tos) <- Map.assocs graph, to <- tos]) |