Welcome! Please see the About page for a little more info on how this works.

+6 votes
in Sequences by
edited by

I found to my surprise that distinct throws an exception when called on a set, e.g.
(distinct #{1 2 3})

A workaround is to call seq on the set first: (distinct (seq #{1 2 3}))

However this seems unneccesary ceremony when every other sequence operator I can think of - map, keep, reduce, first, some etc will accept sets .

Furthermore the docstring for distinct states:
> Returns a lazy sequence of the elements of coll with duplicates removed.

As (coll? #{1 2 3}) => true this is confusing for the user to understand.

There has been some discussion on this topic related to its emergence in math.combinatorics here and here without any real decision.

I propose that distinct should accept any seqable coll.

1 Answer

+2 votes

Sets are already distinct - why would you call distinct on it?

If you had a function that took a collection you wouldn't necessarily know it was a set or a vector, right? Unless you tested it within the function body.
I would just rephrase "I propose that distinct should support sets internally." to just "distinct should accept any seqable coll".
Stan is correct, my enclosing function accepts all collections and it is important that I get distinct values whilst preserving the order. I have updated my question with your proposed wording Alex, thank you both.