on ab5bd5d help sign in
patch/src/Data/Patch/IntMap.hs
61 lines 3.0 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 PatchHet (PatchIntMap a) where
23 type PatchSource (PatchIntMap a) = IntMap a
24 type PatchTarget (PatchIntMap a) = IntMap a
25instance Patch (PatchIntMap a) where
26 apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
27 let removes = IntMap.filter isNothing p
28 adds = IntMap.mapMaybe id p
29 in IntMap.union adds $ v `IntMap.difference` removes
31-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
32-- If the same key is modified by both patches, the one on the left will take
33-- precedence.
34instance Semigroup (PatchIntMap v) where
35 PatchIntMap a <> PatchIntMap b = PatchIntMap $ a `mappend` b --TODO: Add a semigroup instance for Map
36 -- PatchMap is idempotent, so stimes n is id for every n
37 stimes = stimesIdempotentMonoid
39-- | Map a function @Int -> a -> b@ over all @a@s in the given @'PatchIntMap' a@
40-- (that is, all inserts/updates), producing a @PatchIntMap b@.
41mapIntMapPatchWithKey :: (Int -> a -> b) -> PatchIntMap a -> PatchIntMap b
42mapIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap $ IntMap.mapWithKey (\ k mv -> f k <$> mv) m
44-- | Map an effectful function @Int -> a -> f b@ over all @a@s in the given @'PatchIntMap' a@
45-- (that is, all inserts/updates), producing a @f (PatchIntMap b)@.
46traverseIntMapPatchWithKey :: Applicative f => (Int -> a -> f b) -> PatchIntMap a -> f (PatchIntMap b)
47traverseIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap <$> IntMap.traverseWithKey (\k mv -> traverse (f k) mv) m
49-- | Extract all @a@s inserted/updated by the given @'PatchIntMap' a@.
50patchIntMapNewElements :: PatchIntMap a -> [a]
51patchIntMapNewElements (PatchIntMap m) = catMaybes $ IntMap.elems m
53-- | Convert the given @'PatchIntMap' a@ into an @'IntMap' a@ with all
54-- the inserts/updates in the given patch.
55patchIntMapNewElementsMap :: PatchIntMap a -> IntMap a
56patchIntMapNewElementsMap (PatchIntMap m) = IntMap.mapMaybe id m
58-- | Subset the given @'IntMap' a@ to contain only the keys that would be
59-- deleted by the given @'PatchIntMap' a@.
60getDeletions :: PatchIntMap v -> IntMap v' -> IntMap v'
61getDeletions (PatchIntMap m) v = IntMap.intersection v m