@@ -49,6 +49,8 @@ module Data.Map.Internal
4949
5050import Prelude
5151
52+ import Control.Alt (class Alt )
53+ import Control.Plus (class Plus )
5254import Data.Eq (class Eq1 )
5355import Data.Foldable (foldl , foldMap , foldr , class Foldable )
5456import Data.FoldableWithIndex (class FoldableWithIndex , foldlWithIndex , foldrWithIndex )
@@ -90,11 +92,11 @@ instance ordMap :: (Ord k, Ord v) => Ord (Map k v) where
9092instance showMap :: (Show k , Show v ) => Show (Map k v ) where
9193 show m = " (fromFoldable " <> show (toAscArray m) <> " )"
9294
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
9597
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
98100
99101instance functorMap :: Functor (Map k ) where
100102 map _ Leaf = Leaf
@@ -122,9 +124,6 @@ instance foldableWithIndexMap :: FoldableWithIndex k (Map k) where
122124 foldrWithIndex f z m = foldr (uncurry f) z $ asList $ toUnfoldable m
123125 foldMapWithIndex f m = foldMap (uncurry f) $ asList $ toUnfoldable m
124126
125- asList :: forall k v . List (Tuple k v ) -> List (Tuple k v )
126- asList = identity
127-
128127instance traversableMap :: Traversable (Map k ) where
129128 traverse f Leaf = pure Leaf
130129 traverse f (Two left k v right) =
@@ -158,6 +157,9 @@ instance traversableWithIndexMap :: TraversableWithIndex k (Map k) where
158157 <*> f k2 v2
159158 <*> traverseWithIndex f right
160159
160+ asList :: forall k v . List (Tuple k v ) -> List (Tuple k v )
161+ asList = identity
162+
161163-- | Render a `Map` as a `String`
162164showTree :: forall k v . Show k => Show v => Map k v -> String
163165showTree Leaf = " Leaf"
@@ -322,7 +324,10 @@ findMin = go Nothing
322324-- | == ["zero", "one", "two"]
323325-- | ```
324326foldSubmap :: 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 =
326331 let
327332 tooSmall =
328333 case kmin of
@@ -367,17 +372,17 @@ foldSubmap kmin kmax f =
367372 -- function because of strictness.
368373 go = case _ of
369374 Leaf ->
370- mempty
375+ memptyValue
371376 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)
375380 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)
381386 in
382387 go
383388
@@ -408,7 +413,7 @@ foldSubmap kmin kmax f =
408413-- | else not (member key m')
409414-- | ```
410415submap :: 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
412417
413418-- | Test if a key is a member of a map
414419member :: forall k v . Ord k => k -> Map k v -> Boolean
0 commit comments