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

Commit 8185cf7

Browse files
committed
Added toAscList as synonym of current toList
1 parent c673935 commit 8185cf7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Data/Map.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Data.Map
1919
, fromFoldable
2020
, fromFoldableWith
2121
, toList
22+
, toAscList
2223
, toUnfoldable
2324
, delete
2425
, pop
@@ -384,6 +385,10 @@ toList Leaf = Nil
384385
toList (Two left k v right) = toList left <> Tuple k v : toList right
385386
toList (Three left k1 v1 mid k2 v2 right) = toList left <> Tuple k1 v1 : toList mid <> Tuple k2 v2 : toList right
386387

388+
-- | Convert a map to a list of key/value pairs where the keys are in ascending order
389+
toAscList :: forall k v. Map k v -> List (Tuple k v)
390+
toAscList = toList
391+
387392
-- | Convert a map to an unfoldable structure of key/value pairs
388393
toUnfoldable :: forall f k v. Unfoldable f => Map k v -> f (Tuple k v)
389394
toUnfoldable m = unfoldr go (m : Nil) where

test/Test/Data/Map.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ mapTests = do
218218
groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
219219
M.fromFoldableWith (<>) arr === f (arr :: List (Tuple String String))
220220

221+
log "toAscList is sorted version of toList"
222+
quickCheck $ \(TestMap m) ->
223+
let list = M.toList (m :: M.Map SmallKey Int)
224+
ascList = M.toAscList m
225+
in ascList === sortBy (compare `on` fst) list
226+
221227
log "Lookup from union"
222228
quickCheck $ \(TestMap m1) (TestMap m2) k ->
223229
M.lookup (smallKey k) (M.union m1 m2) == (case M.lookup k m1 of

0 commit comments

Comments
 (0)