From b8b7bb2033bd57739c8197d3bb6724dd4d2dce18 Mon Sep 17 00:00:00 2001 From: Denis Buzdalov Date: Mon, 23 Sep 2024 18:36:34 +0300 Subject: [PATCH] [ base ] Deprecate `toList` functions for sorted sets and maps For sorted maps a `kvList` should be used instead, for sorted sets use `toList` from `prelude`. --- CHANGELOG_NEXT.md | 4 ++++ libs/base/Data/SortedMap.idr | 25 +++++++++++++++---------- libs/base/Data/SortedMap/Dependent.idr | 23 +++++++++++++++-------- libs/base/Data/SortedSet.idr | 22 ++++++++++++---------- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index 440660c3f6..0463ff6146 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -223,6 +223,10 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO * Added `Data.IORef.atomically` for the chez backend. +* Deprecated `toList` function in favor of `kvList` function in `Data.SortedMap` and `Data.SortedMap.Dependent`. + +* Deprecated `toList` function in favor of `Prelude.toList` in `Data.SortedSet`. + #### Contrib * `Data.List.Lazy` was moved from `contrib` to `base`. diff --git a/libs/base/Data/SortedMap.idr b/libs/base/Data/SortedMap.idr index 621ae02485..cab5c2238c 100644 --- a/libs/base/Data/SortedMap.idr +++ b/libs/base/Data/SortedMap.idr @@ -3,8 +3,6 @@ module Data.SortedMap import Data.SortedMap.Dependent import Data.Zippable -%hide Prelude.toList - export record SortedMap k v where constructor M @@ -109,19 +107,26 @@ export fromListWith : Ord k => (v -> v -> v) -> List (k, v) -> SortedMap k v fromListWith f = flip (insertFromWith f) empty +||| Returns a list of key-value pairs stored in this map export +kvList : SortedMap k v -> List (k, v) +kvList = map unDPair . kvList . unM + +-- Remove as soon as 0.8.0 (or greater) is released +%deprecate -- Use `kvList` instead +public export %inline toList : SortedMap k v -> List (k, v) -toList = map unDPair . toList . unM +toList = kvList ||| Gets the keys of the map. export keys : SortedMap k v -> List k -keys = map fst . toList +keys = map fst . kvList ||| Gets the values of the map. Could contain duplicates. export values : SortedMap k v -> List v -values = map snd . toList +values = map snd . kvList export implementation Functor (SortedMap k) where @@ -142,8 +147,8 @@ implementation Traversable (SortedMap k) where export implementation Ord k => Zippable (SortedMap k) where - zipWith f mx my = fromList $ mapMaybe (\(k, x) => (k,) . f x <$> lookup k my) $ toList mx - zipWith3 f mx my mz = fromList $ mapMaybe (\(k, x) => (k,) .: f x <$> lookup k my <*> lookup k mz) $ toList mx + zipWith f mx my = fromList $ mapMaybe (\(k, x) => (k,) . f x <$> lookup k my) $ kvList mx + zipWith3 f mx my mz = fromList $ mapMaybe (\(k, x) => (k,) .: f x <$> lookup k my <*> lookup k mz) $ kvList mx unzipWith f m = let m' = map f m in (map fst m', map snd m') unzipWith3 f m = let m' = map f m in (map fst m', map (fst . snd) m', map (snd . snd) m') @@ -154,7 +159,7 @@ mergeWith : (v -> v -> v) -> SortedMap k v -> SortedMap k v -> SortedMap k v mergeWith f x y = insertFrom inserted x where inserted : List (k, v) inserted = do - (k, v) <- toList y + (k, v) <- kvList y let v' = (maybe id f $ lookup k x) v pure (k, v') @@ -191,11 +196,11 @@ rightMost = map unDPair . rightMost . unM export (Show k, Show v) => Show (SortedMap k v) where - show m = "fromList " ++ (show $ toList m) + show m = "fromList " ++ (show $ kvList m) export (Eq k, Eq v) => Eq (SortedMap k v) where - (==) = (==) `on` toList + (==) = (==) `on` kvList -- TODO: is this the right variant of merge to use for this? I think it is, but -- I could also see the advantages of using `mergeLeft`. The current approach is diff --git a/libs/base/Data/SortedMap/Dependent.idr b/libs/base/Data/SortedMap/Dependent.idr index 6bc33ea63b..06f1829fa3 100644 --- a/libs/base/Data/SortedMap/Dependent.idr +++ b/libs/base/Data/SortedMap/Dependent.idr @@ -308,19 +308,26 @@ export fromList : Ord k => List (x : k ** v x) -> SortedDMap k v fromList = foldl insert' empty +||| Returns a list of key-value pairs stored in this map export +kvList : SortedDMap k v -> List (x : k ** v x) +kvList Empty = [] +kvList (M _ t) = treeToList t + +-- Remove as soon as 0.8.0 (or greater) is released +%deprecate -- Use `kvList` instead +public export %inline toList : SortedDMap k v -> List (x : k ** v x) -toList Empty = [] -toList (M _ t) = treeToList t +toList = kvList ||| Gets the keys of the map. export keys : SortedDMap k v -> List k -keys = map fst . toList +keys = map fst . kvList export values : SortedDMap k v -> List (x : k ** v x) -values = toList +values = kvList treeMap : ({x : k} -> a x -> b x) -> Tree n k a o -> Tree n k b o treeMap f (Leaf k v) = Leaf k (f v) @@ -385,7 +392,7 @@ mergeWith : DecEq k => ({x : k} -> v x -> v x -> v x) -> SortedDMap k v -> Sorte mergeWith f x y = insertFrom inserted x where inserted : List (x : k ** v x) inserted = do - (k ** v) <- toList y + (k ** v) <- kvList y let v' = (maybe id f $ lookupPrecise k x) v pure (k ** v') @@ -453,15 +460,15 @@ rightMost (M _ t) = Just $ treeRightMost t export (Show k, {x : k} -> Show (v x)) => Show (SortedDMap k v) where - show m = "fromList " ++ (show $ toList m) + show m = "fromList " ++ (show $ kvList m) export (DecEq k, {x : k} -> Eq (v x)) => Eq (SortedDMap k v) where - (==) = (==) `on` toList + (==) = (==) `on` kvList export strictSubmap : DecEq k => ({x : k} -> Eq (v x)) => (sub : SortedDMap k v) -> (sup : SortedDMap k v) -> Bool -strictSubmap sub sup = all (\(k ** v) => Just v == lookupPrecise k sup) $ toList sub +strictSubmap sub sup = all (\(k ** v) => Just v == lookupPrecise k sup) $ kvList sub -- TODO: is this the right variant of merge to use for this? I think it is, but -- I could also see the advantages of using `mergeLeft`. The current approach is diff --git a/libs/base/Data/SortedSet.idr b/libs/base/Data/SortedSet.idr index a90d4a791c..d39ba797b0 100644 --- a/libs/base/Data/SortedSet.idr +++ b/libs/base/Data/SortedSet.idr @@ -4,8 +4,6 @@ import Data.Maybe import Data.SortedMap import Data.SortedMap.Dependent -%hide Prelude.toList - export data SortedSet k = SetWrapper (Data.SortedMap.SortedMap k ()) @@ -41,18 +39,22 @@ export fromList : Ord k => List k -> SortedSet k fromList l = SetWrapper (Data.SortedMap.fromList (map (\i => (i, ())) l)) -export -toList : SortedSet k -> List k -toList (SetWrapper m) = keys m - export Foldable SortedSet where - foldr f z = foldr f z . Data.SortedSet.toList - foldl f z = foldl f z . Data.SortedSet.toList + foldr f z = foldr f z . toList + foldl f z = foldl f z . toList null (SetWrapper m) = null m - foldMap f = foldMap f . Data.SortedSet.toList + foldMap f = foldMap f . toList + + toList (SetWrapper m) = keys m + +-- Remove as soon as 0.8.0 (or greater) is released +%deprecate -- Use `Prelude.toList` instead +public export %inline +toList : SortedSet k -> List k +toList = Prelude.toList ||| Set union. Inserts all elements of x into y export @@ -98,7 +100,7 @@ Eq k => Eq (SortedSet k) where export Show k => Show (SortedSet k) where - show m = "fromList " ++ (show $ toList m) + show m = "fromList " ++ show (Prelude.toList m) export keySet : SortedMap k v -> SortedSet k