summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2019-12-14 09:53:22 +0100
committertomsmeding <tom.smeding@gmail.com>2019-12-14 09:55:57 +0100
commitc44ae0b58e402ebb3240edf15b5241180cd634fc (patch)
treebe0eec1ba3df49a516b0c1c8d60c8eb6b8b127af
parent58e8fb619b63b7588fc370b16c0574feee7ab1cb (diff)
Simplify oppositeGraph
-rw-r--r--Util.hs9
1 files changed, 2 insertions, 7 deletions
diff --git a/Util.hs b/Util.hs
index 9c3fbf0..55c61ca 100644
--- a/Util.hs
+++ b/Util.hs
@@ -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])