2{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE StandaloneDeriving #-}
7{-# LANGUAGE TemplateHaskell #-}
8{-# LANGUAGE TypeFamilies #-}
10-- | Module containing 'PatchIntMap', a 'Patch' for 'IntMap' which allows for
11-- insert/update or delete of associations.
12module Data.Patch.IntMap where
15import Data.IntMap.Strict (IntMap)
16import qualified Data.IntMap.Strict as IntMap
18import Data.Monoid.DecidablyEmpty
19#if !MIN_VERSION_base(4,11,0)
20import Data.Semigroup (Semigroup (..))
22import Data.Patch.Class
24-- | 'Patch' for 'IntMap' which represents insertion or deletion of keys in the mapping.
25-- Internally represented by 'IntMap (Maybe a)', where @Just@ means insert/update
26-- and @Nothing@ means delete.
27newtype PatchIntMap a = PatchIntMap { unPatchIntMap :: IntMap (Maybe a) }
28 deriving ( Show, Read, Eq, Ord
29 , Functor, Foldable, Traversable
30 , Monoid, DecidablyEmpty
33-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
34-- If the same key is modified by both patches, the one on the left will take
36deriving instance Semigroup (PatchIntMap v)
38makeWrapped ''PatchIntMap
40-- | Apply the insertions or deletions to a given 'IntMap'.
41instance Patch (PatchIntMap a) where
42 type PatchTarget (PatchIntMap a) = IntMap a
43 apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
44 let removes = IntMap.filter isNothing p
45 adds = IntMap.mapMaybe id p
46 in IntMap.union adds $ v `IntMap.difference` removes
48instance FunctorWithIndex Int PatchIntMap
49instance FoldableWithIndex Int PatchIntMap
50instance TraversableWithIndex Int PatchIntMap where
51 itraverse = itraversed . Indexed
52 itraversed = _Wrapped .> itraversed <. traversed
54-- | Map a function @Int -> a -> b@ over all @a@s in the given @'PatchIntMap' a@
55-- (that is, all inserts/updates), producing a @PatchIntMap b@.
56mapIntMapPatchWithKey :: (Int -> a -> b) -> PatchIntMap a -> PatchIntMap b
57mapIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap $ IntMap.mapWithKey (\ k mv -> f k <$> mv) m
59-- | Map an effectful function @Int -> a -> f b@ over all @a@s in the given @'PatchIntMap' a@
60-- (that is, all inserts/updates), producing a @f (PatchIntMap b)@.
61traverseIntMapPatchWithKey :: Applicative f => (Int -> a -> f b) -> PatchIntMap a -> f (PatchIntMap b)
62traverseIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap <$> IntMap.traverseWithKey (traverse . f) m
64-- | Extract all @a@s inserted/updated by the given @'PatchIntMap' a@.
65patchIntMapNewElements :: PatchIntMap a -> [a]
66patchIntMapNewElements (PatchIntMap m) = catMaybes $ IntMap.elems m
68-- | Convert the given @'PatchIntMap' a@ into an @'IntMap' a@ with all
69-- the inserts/updates in the given patch.
70patchIntMapNewElementsMap :: PatchIntMap a -> IntMap a
71patchIntMapNewElementsMap (PatchIntMap m) = IntMap.mapMaybe id m
73-- | Subset the given @'IntMap' a@ to contain only the keys that would be
74-- deleted by the given @'PatchIntMap' a@.
75getDeletions :: PatchIntMap v -> IntMap v' -> IntMap v'
76getDeletions (PatchIntMap m) v = IntMap.intersection v m
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list