containers icon indicating copy to clipboard operation
containers copied to clipboard

Add NonEmpty versions of ListUtils

Open brandon-leapyear opened this issue 3 years ago • 5 comments

Could either be done in a new Data.Containers.ListUtils.NonEmpty module or just suffix the functions with NE.

Could either implement all NE functions in terms of non-NE functions:

nubOrd :: Ord a => [a] -> [a]
nubOrd = ...

nubOrdNE :: Ord a => NonEmpty a -> NonEmpty a
nubOrdNE (a :| as) = a :| nubOrd (filter (/= a) as)

or copy the implementations and just implement the lowest NE helper in terms of the non-NE helper:

nubOrd :: Ord a => [a] -> [a]
nubOrd = nubOrdOn id

nubOrdNE :: Ord a => NonEmpty a -> NonEmpty a
nubOrdNE = nubOrdOnNE id

nubOrdOnExcluding :: Ord b => (a -> b) -> Set b -> [a] -> [a]
nubOrdOnExcluding = ...

nubOrdOnExcludingNE :: Ord b => (a -> b) -> Set b -> NonEmpty a -> NonEmpty a
nubOrdOnExcludingNE f s (a :| as) = a :| nubOrdOnExcluding f (Set.insert (f a) s) as

Related: #465

brandon-leapyear avatar Nov 07 '22 23:11 brandon-leapyear

I think a separate module would probably be nicer. What do you think? The second implementation option is definitely the right one.

treeowl avatar Nov 07 '22 23:11 treeowl

base will include nubOrd and nubOrdBy in Data.List and Data.List.NonEmpty starting from version 4.23.0.0: https://github.com/haskell/core-libraries-committee/issues/336, https://github.com/ghc/ghc/commit/e4f6627

fishtreesugar avatar Oct 12 '25 20:10 fishtreesugar

base will include nubOrd and nubOrdBy in Data.List and Data.List.NonEmpty starting from version 4.23.0.0: haskell/core-libraries-committee#336, ghc/ghc@e4f6627

That uses a rather different underlying set implementation. Do you know how its performance compares?

treeowl avatar Oct 12 '25 20:10 treeowl

base will include nubOrd and nubOrdBy in Data.List and Data.List.NonEmpty starting from version 4.23.0.0: haskell/core-libraries-committee#336, ghc/ghc@e4f6627

The proposal hasn't been accepted yet, so it might still change.

konsumlamm avatar Oct 12 '25 22:10 konsumlamm