Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 8090ce4

Browse files
committed
apply optimizations to foldlWithIndex
implement foldl in terms of foldlWithIndex. Effect on benchmarks is negligible.
1 parent 4c2e6e4 commit 8090ce4

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/Data/Map.purs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ module Data.Map
4343
import Prelude
4444

4545
import 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)
4848
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
4949
import Data.List (List(..), (:), length, nub)
5050
import 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

101101
instance 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

123123
asList :: forall k v. List (Tuple k v) -> List (Tuple k v)
124124
asList = id

0 commit comments

Comments
 (0)