on jam@fix-docs help sign in
patch/src/Data/Patch/IntMap.hs
59 lines 2.9 kB history raw
1{-# LANGUAGE DeriveFoldable #-}
2{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE DeriveTraversable #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE TypeFamilies #-}
6-- | Module containing 'PatchIntMap', a 'Patch' for 'IntMap' which allows for
7-- insert/update or delete of associations.
8module Data.Patch.IntMap where
10import Data.IntMap.Strict (IntMap)
11import qualified Data.IntMap.Strict as IntMap
12import Data.Maybe
13import Data.Semigroup
14import Data.Patch.Class
16-- | 'Patch' for 'IntMap' which represents insertion or deletion of keys in the mapping.
17-- Internally represented by 'IntMap (Maybe a)', where @Just@ means insert/update
18-- and @Nothing@ means delete.
19newtype PatchIntMap a = PatchIntMap (IntMap (Maybe a)) deriving (Functor, Foldable, Traversable, Monoid)
21-- | Apply the insertions or deletions to a given 'IntMap'.
22instance Patch (PatchIntMap a) where
23 type PatchTarget (PatchIntMap a) = IntMap a
24 apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
25 let removes = IntMap.filter isNothing p
26 adds = IntMap.mapMaybe id p
27 in IntMap.union adds $ v `IntMap.difference` removes
29-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
30-- If the same key is modified by both patches, the one on the left will take
31-- precedence.
32instance Semigroup (PatchIntMap v) where
33 PatchIntMap a <> PatchIntMap b = PatchIntMap $ a `mappend` b --TODO: Add a semigroup instance for Map
34 -- PatchMap is idempotent, so stimes n is id for every n
35 stimes = stimesIdempotentMonoid
37-- | Map a function @Int -> a -> b@ over all @a@s in the given @'PatchIntMap' a@
38-- (that is, all inserts/updates), producing a @PatchIntMap b@.
39mapIntMapPatchWithKey :: (Int -> a -> b) -> PatchIntMap a -> PatchIntMap b
40mapIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap $ IntMap.mapWithKey (\ k mv -> f k <$> mv) m
42-- | Map an effectful function @Int -> a -> f b@ over all @a@s in the given @'PatchIntMap' a@
43-- (that is, all inserts/updates), producing a @f (PatchIntMap b)@.
44traverseIntMapPatchWithKey :: Applicative f => (Int -> a -> f b) -> PatchIntMap a -> f (PatchIntMap b)
45traverseIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap <$> IntMap.traverseWithKey (\k mv -> traverse (f k) mv) m
47-- | Extract all @a@s inserted/updated by the given @'PatchIntMap' a@.
48patchIntMapNewElements :: PatchIntMap a -> [a]
49patchIntMapNewElements (PatchIntMap m) = catMaybes $ IntMap.elems m
51-- | Convert the given @'PatchIntMap' a@ into an @'IntMap' a@ with all
52-- the inserts/updates in the given patch.
53patchIntMapNewElementsMap :: PatchIntMap a -> IntMap a
54patchIntMapNewElementsMap (PatchIntMap m) = IntMap.mapMaybe id m
56-- | Subset the given @'IntMap' a@ to contain only the keys that would be
57-- deleted by the given @'PatchIntMap' a@.
58getDeletions :: PatchIntMap v -> IntMap v' -> IntMap v'
59getDeletions (PatchIntMap m) v = IntMap.intersection v m