Skip to content

Commit

Permalink
[ base ] Deprecate toList functions for sorted sets and maps
Browse files Browse the repository at this point in the history
For sorted maps a `kvList` should be used instead,
for sorted sets use `toList` from `prelude`.
  • Loading branch information
buzden committed Sep 23, 2024
1 parent f3dca12 commit b8b7bb2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
25 changes: 15 additions & 10 deletions libs/base/Data/SortedMap.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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')

Expand All @@ -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')

Expand Down Expand Up @@ -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
Expand Down
23 changes: 15 additions & 8 deletions libs/base/Data/SortedMap/Dependent.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions libs/base/Data/SortedSet.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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 ())

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b8b7bb2

Please sign in to comment.