summaryrefslogtreecommitdiffstats
path: root/src/Much/MappedSets.hs
diff options
context:
space:
mode:
authorKierán Meinhardt <kieran.meinhardt@gmail.com>2020-09-23 17:44:40 +0200
committerKierán Meinhardt <kieran.meinhardt@gmail.com>2020-09-23 17:44:40 +0200
commit8e92e6e11d2b3b0bfb5ac9d68f347219493e6380 (patch)
tree6484ca42d85ca89475e922f7b45039c116ebbf97 /src/Much/MappedSets.hs
parent6a6ad3aecd53ffd89101a0dee2b4ea576d4964d4 (diff)
split into library + executables
Diffstat (limited to 'src/Much/MappedSets.hs')
-rw-r--r--src/Much/MappedSets.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/Much/MappedSets.hs b/src/Much/MappedSets.hs
new file mode 100644
index 0000000..ec0ae73
--- /dev/null
+++ b/src/Much/MappedSets.hs
@@ -0,0 +1,28 @@
+module Much.MappedSets (invert, mk) where
+
+import Control.Arrow
+import Data.Map.Strict (Map)
+import qualified Data.Map.Strict as Map
+import Data.Maybe
+import Data.Set (Set)
+import qualified Data.Set as Set
+
+
+mk :: (Ord a, Ord b) => [(a, [b])] -> Map a (Set b)
+mk =
+ Map.fromList . map (second Set.fromList)
+
+
+invert :: (Ord a, Ord b) => Map a (Set b) -> Map b (Set a)
+invert =
+ Map.foldrWithKey invert1 Map.empty
+
+
+invert1 :: (Ord a, Ord b) => a -> Set b -> Map b (Set a) -> Map b (Set a)
+invert1 k v a =
+ Set.foldr (upsert k) a v
+
+
+upsert :: (Ord a, Ord b) => a -> b -> Map b (Set a) -> Map b (Set a)
+upsert k =
+ Map.alter (Just . Set.insert k . fromMaybe Set.empty)