# Better support for sets

0 votes
It seems reasonable to want to use a clojure set as the items parameter for the combinatoric functions. However, in the case of {{(combinations some-set 1)}}, an exception is raised because Clojure's distinct function (called when {{(= t 1)}}) does not support sets. This problem can be worked around by converting the set to a seq before calling combinations.

For all the other functions, it appears extending {{all-different?}} with a {{set?}} check would make the functions slightly more efficient (by avoiding the linear {{distinct?}} scan).

## 8 Answers

0 votes
by

Comment made by: glchapman

I looked a little further: it looks like {{nth-permutation-distinct}} will call {{nth}} on its {{l}} parameter. Sets do not support {{nth}}, so better set support would require a change to that function.

0 votes
by

Comment made by: markengelberg

See discussion here: https://groups.google.com/forum/#!topic/clojure/XUEqdCSI6c4

I don't think it necessarily makes sense for sets to be valid inputs. What these algorithms return is highly dependent on the order of elements in the seq. Since sets are unordered, such a change would make it so that these are not pure functions, i.e., they could return completely different outputs for the same (equal) inputs. That's because two sets that compare as equal may return a different ordering of elements when applying seq. This could wreak havoc with people's test cases in their functions which call these with sets, etc.

I think it is better for the user to be forced to apply seq directly rather than have it happen implicitly, so he/she can think about the consequences, and maybe do a sort of the seq or something to ensure a consistent output, if desired.

0 votes
by

Comment made by: nathan

Fix or no fix, the least that needs to happen is an edit to the docstring.

As it is, combinations is going to work just fine until the size-1 corner-case hits. Hidden land-mines like this need some kind of fix.
Forcing action on ordering issues would entail throwing an exception, popping a warning, or making functions error on sets consistently.

0 votes
by

Comment made by: markengelberg

I agree that the current behavior is unfriendly. Possible solutions are:

1) Implicitly call seq on all inputs (disadvantage: no longer pure functions)
2) Throw errors on sets (disadvantage: adds runtime check)
3) Add info to docstring that inputs must be a sequential (disadvantage: need to read docstring to understand constraint, does everyone know what a "sequential" means, and that sets and maps do not qualify?)
4) Add discussion on this point to README (disdavantage: does anyone read the README?)

I'd lean towards 3 or 4. I'm happy to make a change along those lines.

0 votes
by

Comment made by: nathan

Thinking about #1, for anything but size-1 combinations, the behavior wouldn't be any less pure than it is now. It would also have the exact same purity as seq, vec, map or any other order-preserving function. (map inc some-set) has the same caveat for order. Since the issue of equality on order-hiding data-structures and order-preserving (revealing?) functions is a lot bigger than combinatorics functions, a suitable equality-warning really belongs with order-hiding objects themselves.

Thinking about the discussion on the mailing list, one of the reasons people use sets is to deliberately ignore order. I think the best advice would be to convert sequences back to sets before testing. (set (map inc some-set))

I think if we wouldn't leave map broken on sets, it's worth fixing here.

0 votes
by

Comment made by: nathan

I see some of the issue with combinations. Mathematical combinations are order-agnostic; but these are lists. For equality-testing you'd use: (set (map set (combinations some-set n)))

0 votes
by

Comment made by: markengelberg

For the time being, I've added a note to the top of the usage notes in the README. The only thing that really breaks down for sets is the call to `distinct`, so if Clojure ever modifies the code for `distinct` so that it works on sets (which seems like it would be a trivial change), then this note can be removed.

0 votes
by
Reference: https://clojure.atlassian.net/browse/MCOMB-8 (reported by glchapman)