haskell / containers

Assorted concrete container types
https://hackage.haskell.org/package/containers
315 stars 178 forks source link

Set.fromAscList and Set.fromDescList keep the first of duplicates, inconsistent with other functions #1032

Open meooow25 opened 2 weeks ago

meooow25 commented 2 weeks ago
λ> import qualified Data.Set as S
λ> import qualified Data.Map as M
λ> import Data.Semigroup (Arg(..))
λ> M.fromList [(Arg 1 2, 2), (Arg 1 3, 3)]
fromList [(Arg 1 3,3)]
λ> M.fromAscList [(Arg 1 2, 2), (Arg 1 3, 3)]
fromList [(Arg 1 3,3)]
λ> M.fromDescList [(Arg 1 2, 2), (Arg 1 3, 3)]
fromList [(Arg 1 3,3)]
λ> S.fromList [Arg 1 2, Arg 1 3]
fromList [Arg 1 3]
λ> S.fromAscList [Arg 1 2, Arg 1 3]
fromList [Arg 1 2] -- ??
λ> S.fromDescList [Arg 1 2, Arg 1 3]
fromList [Arg 1 2] -- ??

We should change this to be consistent.

meooow25 commented 2 weeks ago

Unlike Map, Set documentation for any of the fromList functions does not specify which element will be kept in case of duplicates. If we make it consistent, we should also mention it in the docs.

Edit: In fact Map doesn't specify which key will be kept, only the value. But I suppose it would be weird if those did not match.

Are there any advantages in keeping the behavior unspecified? ...I can't think of any.