summaryrefslogtreecommitdiff
path: root/MonMap.hs
blob: dd70ec3f19362f55cf9f246df545c00c5dc65246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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