git.iko.soy/monoidal-containers
on master help sign in
More instances
commit0ff0d5ee45d2b633e01226e0bdf5b3ac759107e3 authorBen Gamari <bgamari.foss@gmail.com> dateTue 20 Jan 2015, 11:55 (12 years ago) parent981ee3c
1 file changed+21
src/Data/Map/Monoidal.hs+21
src/Data/Map/Monoidal.hs+21
@@ -5,6 +5,7 @@
55{-# LANGUAGE GeneralizedNewtypeDeriving #-}
66{-# LANGUAGE DeriveTraversable #-}
77{-# LANGUAGE DeriveDataTypeable #-}
8+{-# LANGUAGE StandaloneDeriving #-}
89
910-- | This module provides a 'Data.Map' variant which uses the value's
1011-- 'Monoid' instance to accumulate conflicting entries when merging
@@ -50,8 +51,28 @@ instance Ord k => Ixed (MonoidalMap k a) where
5051 Nothing -> pure (MM m)
5152 {-# INLINE ix #-}
5253
54+instance Ord k => At (MonoidalMap k a) where
55+ at k f (MM m) = f mv <&> \r -> case r of
56+ Nothing -> maybe (MM m) (const (MM $ M.delete k m)) mv
57+ Just v' -> MM $ M.insert k v' m
58+ where mv = M.lookup k m
59+ {-# INLINE at #-}
60+
5361instance Each (MonoidalMap k a) (MonoidalMap k b) a b
5462
63+instance Ord k => FunctorWithIndex k (MonoidalMap k)
64+instance Ord k => FoldableWithIndex k (MonoidalMap k)
65+instance Ord k => TraversableWithIndex k (MonoidalMap k) where
66+ itraverse f (MM m) = fmap MM $ itraverse f m
67+ {-# INLINE itraverse #-}
68+
69+instance Ord k => TraverseMin k (MonoidalMap k) where
70+ traverseMin f (MM m) = fmap MM $ traverseMin f m
71+ {-# INLINE traverseMin #-}
72+instance Ord k => TraverseMax k (MonoidalMap k) where
73+ traverseMax f (MM m) = fmap MM $ traverseMax f m
74+ {-# INLINE traverseMax #-}
75+
5576instance AsEmpty (MonoidalMap k a) where
5677 _Empty = nearly (MM M.empty) (M.null . unpack)
5778 {-# INLINE _Empty #-}