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