2{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE TypeFamilies #-}
4-- | 'Patch'es on 'Map' that consist only of insertions (including overwrites)
6module Reflex.Patch.Map where
8import Reflex.Patch.Class
11import qualified Data.Map as Map
15-- | A set of changes to a 'Map'. Any element may be inserted/updated or
16-- deleted. Insertions are represented as values wrapped in 'Just', while
17-- deletions are represented as 'Nothing's
18newtype PatchMap k v = PatchMap { unPatchMap :: Map k (Maybe v) }
19 deriving (Show, Read, Eq, Ord)
21-- | Apply the insertions or deletions to a given 'Map'.
22instance Ord k => Patch (PatchMap k v) where
23 type PatchTarget (PatchMap k v) = Map k v
24 {-# INLINABLE apply #-}
25 apply (PatchMap p) old = Just $! insertions `Map.union` (old `Map.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
26 where insertions = Map.mapMaybeWithKey (const id) p
27 deletions = Map.mapMaybeWithKey (const nothingToJust) p
32-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
33-- If the same key is modified by both patches, the one on the left will take
35instance Ord k => Semigroup (PatchMap k v) where
36 PatchMap a <> PatchMap b = PatchMap $ a `mappend` b --TODO: Add a semigroup instance for Map
37 -- PatchMap is idempotent, so stimes n is id for every n
38#if MIN_VERSION_semigroups(0,17,0)
39 stimes = stimesIdempotentMonoid
41 times1p n x = case compare n 0 of
42 LT -> error "stimesIdempotentMonoid: negative multiplier"
47-- | The empty 'PatchMap' contains no insertions or deletions
48instance Ord k => Monoid (PatchMap k v) where
49 mempty = PatchMap mempty
52-- | 'fmap'ping a 'PatchMap' will alter all of the values it will insert.
53-- Deletions are unaffected.
54instance Functor (PatchMap k) where
55 fmap f = PatchMap . fmap (fmap f) . unPatchMap
57-- | Returns all the new elements that will be added to the 'Map'
58patchMapNewElements :: PatchMap k v -> [v]
59patchMapNewElements (PatchMap p) = catMaybes $ Map.elems p
61-- | Returns all the new elements that will be added to the 'Map'
62patchMapNewElementsMap :: PatchMap k v -> Map k v
63patchMapNewElementsMap (PatchMap p) = Map.mapMaybe id p
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