@@ -41,14 +41,18 @@ module Data.Map
41
41
) where
42
42
43
43
import Prelude
44
+
44
45
import Data.Eq (class Eq1 )
45
46
import Data.Foldable (foldl , foldMap , foldr , class Foldable )
47
+ import Data.FoldableWithIndex (class FoldableWithIndex )
48
+ import Data.FunctorWithIndex (class FunctorWithIndex , mapWithIndex )
46
49
import Data.List (List (..), (:), length , nub )
47
50
import Data.List.Lazy as LL
48
51
import Data.Maybe (Maybe (..), maybe , isJust , fromMaybe )
49
52
import Data.Monoid (class Monoid , mempty )
50
53
import Data.Ord (class Ord1 )
51
54
import Data.Traversable (traverse , class Traversable )
55
+ import Data.TraversableWithIndex (class TraversableWithIndex , traverseWithIndex )
52
56
import Data.Tuple (Tuple (Tuple), snd , uncurry )
53
57
import Data.Unfoldable (class Unfoldable , unfoldr )
54
58
import Partial.Unsafe (unsafePartial )
@@ -89,11 +93,24 @@ instance functorMap :: Functor (Map k) where
89
93
map f (Two left k v right) = Two (map f left) k (f v) (map f right)
90
94
map f (Three left k1 v1 mid k2 v2 right) = Three (map f left) k1 (f v1) (map f mid) k2 (f v2) (map f right)
91
95
96
+ instance functorWithIndexMap :: FunctorWithIndex k (Map k ) where
97
+ mapWithIndex _ Leaf = Leaf
98
+ mapWithIndex f (Two left k v right) = Two (mapWithIndex f left) k (f k v) (mapWithIndex f right)
99
+ mapWithIndex f (Three left k1 v1 mid k2 v2 right) = Three (mapWithIndex f left) k1 (f k1 v1) (mapWithIndex f mid) k2 (f k2 v2) (mapWithIndex f right)
100
+
92
101
instance foldableMap :: Foldable (Map k ) where
93
102
foldl f z m = foldl f z (values m)
94
103
foldr f z m = foldr f z (values m)
95
104
foldMap f m = foldMap f (values m)
96
105
106
+ instance foldableWithIndexMap :: FoldableWithIndex k (Map k ) where
107
+ foldlWithIndex f z m = foldl (uncurry <<< (flip f)) z $ asList $ toUnfoldable m
108
+ foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toUnfoldable m
109
+ foldMapWithIndex f m = foldMap (uncurry f) $ asList $ toUnfoldable m
110
+
111
+ asList :: forall k v . List (Tuple k v ) -> List (Tuple k v )
112
+ asList = id
113
+
97
114
instance traversableMap :: Traversable (Map k ) where
98
115
traverse f Leaf = pure Leaf
99
116
traverse f (Two left k v right) =
@@ -111,6 +128,22 @@ instance traversableMap :: Traversable (Map k) where
111
128
<*> traverse f right
112
129
sequence = traverse id
113
130
131
+ instance traversableWithIndexMap :: TraversableWithIndex k (Map k ) where
132
+ traverseWithIndex f Leaf = pure Leaf
133
+ traverseWithIndex f (Two left k v right) =
134
+ Two <$> traverseWithIndex f left
135
+ <*> pure k
136
+ <*> f k v
137
+ <*> traverseWithIndex f right
138
+ traverseWithIndex f (Three left k1 v1 mid k2 v2 right) =
139
+ Three <$> traverseWithIndex f left
140
+ <*> pure k1
141
+ <*> f k1 v1
142
+ <*> traverseWithIndex f mid
143
+ <*> pure k2
144
+ <*> f k2 v2
145
+ <*> traverseWithIndex f right
146
+
114
147
-- | Render a `Map` as a `String`
115
148
showTree :: forall k v . Show k => Show v => Map k v -> String
116
149
showTree Leaf = " Leaf"
0 commit comments