@@ -43,8 +43,8 @@ module Data.Map
43
43
import Prelude
44
44
45
45
import Data.Eq (class Eq1 )
46
- import Data.Foldable (foldl , foldr , foldMapDefaultL , class Foldable )
47
- import Data.FoldableWithIndex (class FoldableWithIndex , foldlWithIndex , foldMapWithIndexDefaultL )
46
+ import Data.Foldable (foldl , foldMapDefaultL , class Foldable )
47
+ import Data.FoldableWithIndex (class FoldableWithIndex , foldlWithIndex , foldrWithIndex , foldMapWithIndexDefaultL )
48
48
import Data.FunctorWithIndex (class FunctorWithIndex , mapWithIndex )
49
49
import Data.List (List (..), (:), length , nub )
50
50
import Data.List.Lazy as LL
@@ -100,7 +100,7 @@ instance functorWithIndexMap :: FunctorWithIndex k (Map k) where
100
100
101
101
instance foldableMap :: Foldable (Map k ) where
102
102
foldl f = foldlWithIndex (const f)
103
- foldr f z m = foldr f z (values m )
103
+ foldr f = foldrWithIndex (const f )
104
104
foldMap = foldMapDefaultL
105
105
106
106
instance foldableWithIndexMap :: FoldableWithIndex k (Map k ) where
@@ -117,7 +117,19 @@ instance foldableWithIndexMap :: FoldableWithIndex k (Map k) where
117
117
go acc (left : singleton k v : right : tl)
118
118
Three left k1 v1 mid k2 v2 right ->
119
119
go acc (left : singleton k1 v1 : mid : singleton k2 v2 : right : tl)
120
- foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toUnfoldable m
120
+ foldrWithIndex f z m = go (m : Nil )
121
+ where
122
+ go Nil = z
123
+ go (hd : tl) = case hd of
124
+ Leaf -> go tl
125
+ Two Leaf k v Leaf ->
126
+ f k v $ go tl
127
+ Two Leaf k v right ->
128
+ f k v $ go $ right : tl
129
+ Two left k v right ->
130
+ go $ left : singleton k v : right : tl
131
+ Three left k1 v1 mid k2 v2 right ->
132
+ go $ left : singleton k1 v1 : mid : singleton k2 v2 : right : tl
121
133
foldMapWithIndex = foldMapWithIndexDefaultL
122
134
123
135
asList :: forall k v . List (Tuple k v ) -> List (Tuple k v )
0 commit comments