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

Commit a4ebe6a

Browse files
authored
Add Functor/Foldable/TraversableWithIndex for Map (#126)
* Add Functor/Foldable/TraversableWithIndex for Map * Don't use unicode
1 parent 0e44e99 commit a4ebe6a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"purescript-lists": "^4.0.0",
2727
"purescript-st": "^3.0.0",
2828
"purescript-gen": "^1.1.0",
29-
"purescript-foldable-traversable": "^3.4.0"
29+
"purescript-foldable-traversable": "^3.6.1"
3030
},
3131
"devDependencies": {
3232
"purescript-quickcheck": "^4.0.0",

src/Data/Map.purs

+33
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ module Data.Map
4141
) where
4242

4343
import Prelude
44+
4445
import Data.Eq (class Eq1)
4546
import Data.Foldable (foldl, foldMap, foldr, class Foldable)
47+
import Data.FoldableWithIndex (class FoldableWithIndex)
48+
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
4649
import Data.List (List(..), (:), length, nub)
4750
import Data.List.Lazy as LL
4851
import Data.Maybe (Maybe(..), maybe, isJust, fromMaybe)
4952
import Data.Monoid (class Monoid, mempty)
5053
import Data.Ord (class Ord1)
5154
import Data.Traversable (traverse, class Traversable)
55+
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
5256
import Data.Tuple (Tuple(Tuple), snd, uncurry)
5357
import Data.Unfoldable (class Unfoldable, unfoldr)
5458
import 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+
92101
instance 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+
97114
instance 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`
115148
showTree :: forall k v. Show k => Show v => Map k v -> String
116149
showTree Leaf = "Leaf"

0 commit comments

Comments
 (0)