@@ -49,6 +49,8 @@ module Data.Map.Internal
49
49
50
50
import Prelude
51
51
52
+ import Control.Alt (class Alt )
53
+ import Control.Plus (class Plus )
52
54
import Data.Eq (class Eq1 )
53
55
import Data.Foldable (foldl , foldMap , foldr , class Foldable )
54
56
import Data.FoldableWithIndex (class FoldableWithIndex , foldlWithIndex , foldrWithIndex )
@@ -90,11 +92,11 @@ instance ordMap :: (Ord k, Ord v) => Ord (Map k v) where
90
92
instance showMap :: (Show k , Show v ) => Show (Map k v ) where
91
93
show m = " (fromFoldable " <> show (toAscArray m) <> " )"
92
94
93
- instance semigroupMap :: Ord k => Semigroup (Map k v ) where
94
- append = union
95
+ instance altMap :: Ord k => Alt (Map k ) where
96
+ alt = union
95
97
96
- instance monoidMap :: Ord k => Monoid (Map k v ) where
97
- mempty = empty
98
+ instance plusMap :: Ord k => Plus (Map k ) where
99
+ empty = empty
98
100
99
101
instance functorMap :: Functor (Map k ) where
100
102
map _ Leaf = Leaf
@@ -122,9 +124,6 @@ instance foldableWithIndexMap :: FoldableWithIndex k (Map k) where
122
124
foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toUnfoldable m
123
125
foldMapWithIndex f m = foldMap (uncurry f) $ asList $ toUnfoldable m
124
126
125
- asList :: forall k v . List (Tuple k v ) -> List (Tuple k v )
126
- asList = identity
127
-
128
127
instance traversableMap :: Traversable (Map k ) where
129
128
traverse f Leaf = pure Leaf
130
129
traverse f (Two left k v right) =
@@ -158,6 +157,9 @@ instance traversableWithIndexMap :: TraversableWithIndex k (Map k) where
158
157
<*> f k2 v2
159
158
<*> traverseWithIndex f right
160
159
160
+ asList :: forall k v . List (Tuple k v ) -> List (Tuple k v )
161
+ asList = identity
162
+
161
163
-- | Render a `Map` as a `String`
162
164
showTree :: forall k v . Show k => Show v => Map k v -> String
163
165
showTree Leaf = " Leaf"
@@ -322,7 +324,10 @@ findMin = go Nothing
322
324
-- | == ["zero", "one", "two"]
323
325
-- | ```
324
326
foldSubmap :: forall k v m . Ord k => Monoid m => Maybe k -> Maybe k -> (k -> v -> m ) -> Map k v -> m
325
- foldSubmap kmin kmax f =
327
+ foldSubmap = foldSubmapBy (<>) mempty
328
+
329
+ foldSubmapBy :: forall k v m . Ord k => (m -> m -> m ) -> m -> Maybe k -> Maybe k -> (k -> v -> m ) -> Map k v -> m
330
+ foldSubmapBy appendFn memptyValue kmin kmax f =
326
331
let
327
332
tooSmall =
328
333
case kmin of
@@ -367,17 +372,17 @@ foldSubmap kmin kmax f =
367
372
-- function because of strictness.
368
373
go = case _ of
369
374
Leaf ->
370
- mempty
375
+ memptyValue
371
376
Two left k v right ->
372
- (if tooSmall k then mempty else go left)
373
- <> (if inBounds k then f k v else mempty )
374
- <> (if tooLarge k then mempty else go right)
377
+ (if tooSmall k then memptyValue else go left)
378
+ `appendFn` (if inBounds k then f k v else memptyValue )
379
+ `appendFn` (if tooLarge k then memptyValue else go right)
375
380
Three left k1 v1 mid k2 v2 right ->
376
- (if tooSmall k1 then mempty else go left)
377
- <> (if inBounds k1 then f k1 v1 else mempty )
378
- <> (if tooSmall k2 || tooLarge k1 then mempty else go mid)
379
- <> (if inBounds k2 then f k2 v2 else mempty )
380
- <> (if tooLarge k2 then mempty else go right)
381
+ (if tooSmall k1 then memptyValue else go left)
382
+ `appendFn` (if inBounds k1 then f k1 v1 else memptyValue )
383
+ `appendFn` (if tooSmall k2 || tooLarge k1 then memptyValue else go mid)
384
+ `appendFn` (if inBounds k2 then f k2 v2 else memptyValue )
385
+ `appendFn` (if tooLarge k2 then memptyValue else go right)
381
386
in
382
387
go
383
388
@@ -408,7 +413,7 @@ foldSubmap kmin kmax f =
408
413
-- | else not (member key m')
409
414
-- | ```
410
415
submap :: forall k v . Ord k => Maybe k -> Maybe k -> Map k v -> Map k v
411
- submap kmin kmax = foldSubmap kmin kmax singleton
416
+ submap kmin kmax = foldSubmapBy union empty kmin kmax singleton
412
417
413
418
-- | Test if a key is a member of a map
414
419
member :: forall k v . Ord k => k -> Map k v -> Boolean
0 commit comments