@@ -41,14 +41,18 @@ module Data.Map
4141  ) where 
4242
4343import  Prelude 
44+ 
4445import  Data.Eq  (class  Eq1 )
4546import  Data.Foldable  (foldl , foldMap , foldr , class  Foldable )
47+ import  Data.FoldableWithIndex  (class  FoldableWithIndex )
48+ import  Data.FunctorWithIndex  (class  FunctorWithIndex , mapWithIndex )
4649import  Data.List  (List (..), (:), length , nub )
4750import  Data.List.Lazy  as  LL 
4851import  Data.Maybe  (Maybe (..), maybe , isJust , fromMaybe )
4952import  Data.Monoid  (class  Monoid , mempty )
5053import  Data.Ord  (class  Ord1 )
5154import  Data.Traversable  (traverse , class  Traversable )
55+ import  Data.TraversableWithIndex  (class  TraversableWithIndex , traverseWithIndex )
5256import  Data.Tuple  (Tuple (Tuple), snd , uncurry )
5357import  Data.Unfoldable  (class  Unfoldable , unfoldr )
5458import  Partial.Unsafe  (unsafePartial )
@@ -89,11 +93,24 @@ instance functorMap :: Functor (Map k) where
8993  map f (Two  left k v right) = Two  (map f left) k (f v) (map f right)
9094  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)
9195
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+ 
92101instance  foldableMap  :: Foldable  (Map  k ) where 
93102  foldl   f z m = foldl   f z (values m)
94103  foldr   f z m = foldr   f z (values m)
95104  foldMap f   m = foldMap f   (values m)
96105
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+ 
97114instance  traversableMap  :: Traversable  (Map  k ) where 
98115  traverse f Leaf  = pure Leaf 
99116  traverse f (Two  left k v right) =
@@ -111,6 +128,22 @@ instance traversableMap :: Traversable (Map k) where
111128          <*> traverse f right
112129  sequence = traverse id
113130
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+ 
114147--  | Render a `Map` as a `String`
115148showTree  ::  forall  k  v . Show  k  =>  Show  v  =>  Map  k  v  ->  String 
116149showTree Leaf  = " Leaf" 
0 commit comments