foundation icon indicating copy to clipboard operation
foundation copied to clipboard

Add nub family of functions

Open vincenthz opened this issue 9 years ago • 3 comments

nub :: Eq a => [a] -> [a]

to

nub :: Eq (Element c) => c -> c

vincenthz avatar Sep 29 '16 18:09 vincenthz

Use of nub at O(n^2) is nearly always a bug. I have both ordNub and hashNub defined in various places, both of which are substantially more useful. There's pretty much nothing that is just Eq.

ndmitchell avatar Sep 29 '16 19:09 ndmitchell

I agree that nubOrd or nubHash should be used inetead, most of the time. Maybe offer this one as nubEq? Gives it less primacy.

Would this be in a typeclass? Some datatypes can implement nubEq quite efficiently, because they already use Ord or Hashable under the surface. For example, nubEq for a multi hash set could just visit each bucket and nub within the bucket.

mgsloan avatar Sep 29 '16 19:09 mgsloan

@ndmitchell: Agreed. I was definitely considering the Hash variant one already, since I'm adding the hash interfaces now.

@mgsloan: yes, very likely be in a typeclass so that we can offer the family of nub function for unboxed array, boxed array, string, and list. And anything else that will come after that. probably a good idea to still provide a nubEq for some cases, and thus make it less prominent.

vincenthz avatar Sep 29 '16 19:09 vincenthz