@@ -43,8 +43,8 @@ module Data.Map
4343import Prelude
4444
4545import Data.Eq (class Eq1 )
46- import Data.Foldable (foldl , foldMap , foldr , foldMapDefaultL , class Foldable )
47- import Data.FoldableWithIndex (class FoldableWithIndex )
46+ import Data.Foldable (foldl , foldr , foldMapDefaultL , class Foldable )
47+ import Data.FoldableWithIndex (class FoldableWithIndex , foldlWithIndex , foldMapWithIndexDefaultL )
4848import Data.FunctorWithIndex (class FunctorWithIndex , mapWithIndex )
4949import Data.List (List (..), (:), length , nub )
5050import Data.List.Lazy as LL
@@ -99,26 +99,26 @@ instance functorWithIndexMap :: FunctorWithIndex k (Map k) where
9999 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)
100100
101101instance foldableMap :: Foldable (Map k ) where
102- foldl f z m = go z (m : Nil )
102+ foldl f = foldlWithIndex (const f)
103+ foldr f z m = foldr f z (values m)
104+ foldMap = foldMapDefaultL
105+
106+ instance foldableWithIndexMap :: FoldableWithIndex k (Map k ) where
107+ foldlWithIndex f z m = go z (m : Nil )
103108 where
104109 go acc Nil = acc
105110 go acc (hd : tl) = case hd of
106111 Leaf -> go acc tl
107- Two Leaf _ v Leaf ->
108- go (f acc v) tl
109- Two Leaf _ v right ->
110- go (f acc v) (right : tl)
112+ Two Leaf k v Leaf ->
113+ go (f k acc v) tl
114+ Two Leaf k v right ->
115+ go (f k acc v) (right : tl)
111116 Two left k v right ->
112117 go acc (left : singleton k v : right : tl)
113118 Three left k1 v1 mid k2 v2 right ->
114119 go acc (left : singleton k1 v1 : mid : singleton k2 v2 : right : tl)
115- foldr f z m = foldr f z (values m)
116- foldMap = foldMapDefaultL
117-
118- instance foldableWithIndexMap :: FoldableWithIndex k (Map k ) where
119- foldlWithIndex f z m = foldl (uncurry <<< (flip f)) z $ asList $ toUnfoldable m
120120 foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toUnfoldable m
121- foldMapWithIndex f m = foldMap (uncurry f) $ asList $ toUnfoldable m
121+ foldMapWithIndex = foldMapWithIndexDefaultL
122122
123123asList :: forall k v . List (Tuple k v ) -> List (Tuple k v )
124124asList = id
0 commit comments