@@ -21,6 +21,7 @@ module Data.Map
21
21
, fromFoldable
22
22
, fromFoldableWith
23
23
, toUnfoldable
24
+ , unsafeToUnfoldable
24
25
, toAscUnfoldable
25
26
, delete
26
27
, pop
@@ -65,7 +66,7 @@ data Map k v
65
66
66
67
-- Internal use
67
68
toAscArray :: forall k v . Map k v -> Array (Tuple k v )
68
- toAscArray = toAscUnfoldable
69
+ toAscArray = toUnfoldable
69
70
70
71
instance eq1Map :: Eq k => Eq1 (Map k ) where
71
72
eq1 = eq
@@ -554,20 +555,9 @@ fromFoldableWith f = foldl (\m (Tuple k v) -> alter (combine v) k m) empty where
554
555
combine v (Just v') = Just $ f v v'
555
556
combine v Nothing = Just v
556
557
557
- -- | Convert a map to an unfoldable structure of key/value pairs
558
+ -- | Convert a map to an unfoldable structure of key/value pairs where the keys are in ascending order
558
559
toUnfoldable :: forall f k v . Unfoldable f => Map k v -> f (Tuple k v )
559
560
toUnfoldable m = unfoldr go (m : Nil ) where
560
- go Nil = Nothing
561
- go (hd : tl) = case hd of
562
- Leaf -> go tl
563
- Two left k v right ->
564
- Just $ Tuple (Tuple k v) (left : right : tl)
565
- Three left k1 v1 mid k2 v2 right ->
566
- Just $ Tuple (Tuple k1 v1) (singleton k2 v2 : left : mid : right : tl)
567
-
568
- -- | Convert a map to an unfoldable structure of key/value pairs where the keys are in ascending order
569
- toAscUnfoldable :: forall f k v . Unfoldable f => Map k v -> f (Tuple k v )
570
- toAscUnfoldable m = unfoldr go (m : Nil ) where
571
561
go Nil = Nothing
572
562
go (hd : tl) = case hd of
573
563
Leaf -> go tl
@@ -580,6 +570,31 @@ toAscUnfoldable m = unfoldr go (m : Nil) where
580
570
Three left k1 v1 mid k2 v2 right ->
581
571
go $ left : singleton k1 v1 : mid : singleton k2 v2 : right : tl
582
572
573
+ -- | Convert a map to an unfoldable structure of key/value pairs
574
+ --
575
+ -- While this traversal is up to 10% faster in benchmarks than `toUnfoldable`,
576
+ -- it leaks the underlying map stucture, making it only suitable for applications
577
+ -- where order is irrelevant.
578
+ --
579
+ -- If you are unsure, use `toUnfoldable`
580
+ unsafeToUnfoldable :: forall f k v . Unfoldable f => Map k v -> f (Tuple k v )
581
+ unsafeToUnfoldable m = unfoldr go (m : Nil ) where
582
+ go Nil = Nothing
583
+ go (hd : tl) = case hd of
584
+ Leaf -> go tl
585
+ Two left k v right ->
586
+ Just $ Tuple (Tuple k v) (left : right : tl)
587
+ Three left k1 v1 mid k2 v2 right ->
588
+ Just $ Tuple (Tuple k1 v1) (singleton k2 v2 : left : mid : right : tl)
589
+
590
+ -- | Deprecated in favor of `toUnfoldable,` which now also has ascending key order.
591
+ toAscUnfoldable
592
+ :: forall f k v
593
+ . Warn " Map.toAscUnfoldable is deprecated. Use toUnfoldable."
594
+ => Unfoldable f
595
+ => Map k v -> f (Tuple k v )
596
+ toAscUnfoldable = toUnfoldable
597
+
583
598
-- | Get a list of the keys contained in a map
584
599
keys :: forall k v . Map k v -> List k
585
600
keys Leaf = Nil
0 commit comments