1
- {-# LANGUAGE DeriveFoldable #-}
2
- {-# LANGUAGE DeriveFunctor #-}
3
1
{-# LANGUAGE DeriveTraversable #-}
4
2
{-# LANGUAGE FlexibleContexts #-}
3
+ {-# LANGUAGE FlexibleInstances #-}
5
4
{-# LANGUAGE LambdaCase #-}
5
+ {-# LANGUAGE MultiParamTypeClasses #-}
6
6
{-# LANGUAGE PatternGuards #-}
7
7
{-# LANGUAGE ScopedTypeVariables #-}
8
+ {-# LANGUAGE TemplateHaskell #-}
9
+ {-# LANGUAGE TypeApplications #-}
8
10
{-# LANGUAGE TypeFamilies #-}
11
+
9
12
-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
10
13
-- another
11
14
module Data.Patch.MapWithMove where
12
15
13
16
import Data.Patch.Class
14
17
15
18
import Control.Arrow
19
+ import Control.Lens hiding (from , to )
16
20
import Control.Monad.Trans.State
17
21
import Data.Foldable
18
22
import Data.Function
@@ -28,7 +32,13 @@ import Data.Tuple
28
32
-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
29
33
-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
30
34
-- and vice versa. There should never be any unpaired From/To keys.
31
- newtype PatchMapWithMove k v = PatchMapWithMove (Map k (NodeInfo k v )) deriving (Show , Eq , Ord , Functor , Foldable , Traversable )
35
+ newtype PatchMapWithMove k v = PatchMapWithMove
36
+ { -- | Extract the internal representation of the 'PatchMapWithMove'
37
+ unPatchMapWithMove :: Map k (NodeInfo k v )
38
+ }
39
+ deriving ( Show , Read , Eq , Ord
40
+ , Functor , Foldable , Traversable
41
+ )
32
42
33
43
-- | Holds the information about each key: where its new value should come from,
34
44
-- and where its old value should go to
@@ -53,6 +63,13 @@ data From k v
53
63
-- that means it will be deleted.
54
64
type To = Maybe
55
65
66
+ makeWrapped ''PatchMapWithMove
67
+
68
+ instance FunctorWithIndex k (PatchMapWithMove k )
69
+ instance FoldableWithIndex k (PatchMapWithMove k )
70
+ instance TraversableWithIndex k (PatchMapWithMove k ) where
71
+ itraverse f (PatchMapWithMove x) = PatchMapWithMove <$> itraverse (traverse . f) x
72
+
56
73
-- | Create a 'PatchMapWithMove', validating it
57
74
patchMapWithMove :: Ord k => Map k (NodeInfo k v ) -> Maybe (PatchMapWithMove k v )
58
75
patchMapWithMove m = if valid then Just $ PatchMapWithMove m else Nothing
@@ -70,10 +87,6 @@ patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
70
87
, _nodeInfo_to = Nothing
71
88
}
72
89
73
- -- | Extract the internal representation of the 'PatchMapWithMove'
74
- unPatchMapWithMove :: PatchMapWithMove k v -> Map k (NodeInfo k v )
75
- unPatchMapWithMove (PatchMapWithMove p) = p
76
-
77
90
-- | Make a @'PatchMapWithMove' k v@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
78
91
insertMapKey :: k -> v -> PatchMapWithMove k v
79
92
insertMapKey k v = PatchMapWithMove . Map. singleton k $ NodeInfo (From_Insert v) Nothing
0 commit comments