git.iko.soy/monoidal-containers
on master help sign in
More convenience wrappers
commit7e046db04857163916e7765396f947f1c29097db authorBen Gamari <bgamari.foss@gmail.com> dateTue 20 Jan 2015, 12:07 (12 years ago) parent0ff0d5e
1 file changed+27
src/Data/Map/Monoidal.hs+27
src/Data/Map/Monoidal.hs+27
@@ -18,7 +18,12 @@
1818
1919module Data.Map.Monoidal
2020 ( MonoidalMap
21+ -- * Often-needed functions
2122 , singleton
23+ , size
24+ , member
25+ , notMember
26+ , findWithDefault
2227 ) where
2328
2429import Data.Monoid
@@ -107,3 +112,25 @@ instance Ord k => IsList (MonoidalMap k a) where
107112singleton :: Ord k => k -> a -> MonoidalMap k a
108113singleton k a = MM $ M.singleton k a
109114{-# INLINE singleton #-}
115+
116+-- | /O(1)/. The number of elements in the map.
117+size :: MonoidalMap k a -> Int
118+size = M.size . unpack
119+{-# INLINE size #-}
120+
121+-- | /O(log n)/. Is the key a member of the map? See also 'notMember'.
122+member :: Ord k => k -> MonoidalMap k a -> Bool
123+member k = M.member k . unpack
124+{-# INLINE member #-}
125+
126+-- | /O(log n)/. Is the key not a member of the map? See also 'member'.
127+notMember :: Ord k => k -> MonoidalMap k a -> Bool
128+notMember k = not . M.member k . unpack
129+{-# INLINE notMember #-}
130+
131+-- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns
132+-- the value at key @k@ or returns default value @def@
133+-- when the key is not in the map.
134+findWithDefault :: Ord k => a -> k -> MonoidalMap k a -> a
135+findWithDefault def k = M.findWithDefault def k . unpack
136+{-# INLINE findWithDefault #-}