From c44ae0b58e402ebb3240edf15b5241180cd634fc Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 14 Dec 2019 09:53:22 +0100 Subject: Simplify oppositeGraph --- Util.hs | 9 ++------- 1 file 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]) -- cgit v1.2.3