Possible bug in clojure.set/union?

151 views
Skip to first unread message

Randy J. Ray

unread,
Mar 4, 2016, 11:40:28 AM3/4/16
to clo...@googlegroups.com
Hi,

Fairly-new Clojure enthusiast here, currently using Clojure to work through projecteuler.net problems as a means of learning. While using sets on one of the PE problems, I encountered what *might* be a bug. I admit that I haven't searched the backlog of messages from this group or the issues on GitHub, so if this is already known I apologize.

Here is the issue: I discovered that I could pass a list as the second parameter to set/union, and it would be merged into the the set passed as the first parameter and the new resulting set returned. However, if the number of items in the list exceeds the number of items in the set, then the return value is a list with any duplicate elements completely present.

To illustrate, here is a snippet from my REPL:

user=> (require '[clojure.set :as set])
nil
user=> (set/union #{1 2 3} #{2 3 4})
#{1 4 3 2}
user=> (set/union #{1 2 3} #{2 3 4 5})
#{1 4 3 2 5}
user=> (set/union #{1 2 3} (list 2 3 4))
#{1 4 3 2}
user=> (set/union #{1 2 3} (list 2 3 4 5))
(2 3 1 2 3 4 5)

Note that the last expression yields a list of 7 elements rather than a set of 5.

I have not tried this with more than two arguments, so I don't know that would affect the output. I did try putting a list as the first parameter, and that results in a list return value all the time.

Is this a bug? Should I file a GitHub issue on this? I first encountered this in 1.7.0, but I recently updated to 1.8.0 and it is still present.

Randy
--
Randy J. Ray - randy...@gmail.com - twitter.com/rjray
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA

Alex Miller

unread,
Mar 4, 2016, 12:01:58 PM3/4/16
to Clojure, rj...@blackperl.com
Hi Randy,

Several functions in clojure.set allow non-set arguments and this has been an issue with a long history. There have been or are tickets like this, such as http://dev.clojure.org/jira/browse/CLJ-1682 (although this one is re intersection).

This was done somewhat intentionally for performance reasons - rather than validate or transform, just assume they are sets - their behavior when passed non-sets is considered undefined. However, I think this is worth revisiting in light of where we are today vs then. I do not know of a jira specific to clojure.set/union - you are welcome to create one via the contribution process documented at http://clojure.org/community/contributing

Alex

Matching Socks

unread,
Mar 4, 2016, 8:12:28 PM3/4/16
to Clojure, rj...@blackperl.com
This is an FAQ.  It is such a good FAQ that "fixing" it would be a shame.  It's a good FAQ because it starts with a real problem that everyone thinks they have, and it leads (if you doggedly pursue it) to a well-rounded enlightenment as to what makes Clojure interesting.
Reply all
Reply to author
Forward
0 new messages