summaryrefslogtreecommitdiff
path: root/MonMap.hs
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-12-26 18:08:15 +0100
committerTom Smeding <tom.smeding@gmail.com>2020-12-26 18:08:15 +0100
commit5d1d3b4f251bf938648d7d21c6641a1a0cc0768b (patch)
treee237fff9f493c0acc4154c6e77f1a7c3d0f6365f /MonMap.hs
Initial
Diffstat (limited to 'MonMap.hs')
-rw-r--r--MonMap.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/MonMap.hs b/MonMap.hs
new file mode 100644
index 0000000..dd70ec3
--- /dev/null
+++ b/MonMap.hs
@@ -0,0 +1,22 @@
+module MonMap where
+
+import qualified Data.Map.Strict as Map
+
+
+newtype MonMap k v = MonMap (Map.Map k v)
+ deriving (Show)
+
+instance Functor (MonMap k) where
+ fmap f (MonMap m) = MonMap (fmap f m)
+
+instance (Ord k, Semigroup v) => Semigroup (MonMap k v) where
+ MonMap m1 <> MonMap m2 = MonMap (Map.unionWith (<>) m1 m2)
+
+instance (Ord k, Monoid v) => Monoid (MonMap k v) where
+ mempty = MonMap mempty
+
+singleton :: k -> v -> MonMap k v
+singleton = (MonMap .) . Map.singleton
+
+lookup :: Ord k => k -> MonMap k v -> Maybe v
+lookup k (MonMap m) = Map.lookup k m