diff options
author | tv <tv@shackspace.de> | 2015-03-04 01:47:49 +0100 |
---|---|---|
committer | tv <tv@shackspace.de> | 2015-03-04 01:47:49 +0100 |
commit | e885153240f7e6a6737fdb74aa0233966eb19678 (patch) | |
tree | 122d039897587fa4b8b887eedc83cf78c9058024 /MappedSets.hs | |
parent | 04929712f248dbbdf200693c0751dc925fb03c61 (diff) |
test10: initial commit (MBox -> Tree Message)
Diffstat (limited to 'MappedSets.hs')
-rw-r--r-- | MappedSets.hs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/MappedSets.hs b/MappedSets.hs new file mode 100644 index 0000000..c3045c6 --- /dev/null +++ b/MappedSets.hs @@ -0,0 +1,28 @@ +module 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) |