I just got burned by the clojure.set/difference function (v 1.5.1) wherein 
I had in error passed a vector for the second argument. This causes 
inconsistent results depending on the relative lengths of the arguments. 
That is, as soon as the second argument becomes longer than the first we 
get a different branch in the code and the behaviour changes:

user=> (clojure.set/difference #{4 5} [4 5])
#{}
user=> (clojure.set/difference #{4 5} [4 5 6])
#{4 5}
user=> (clojure.set/difference #{:a :b} {:a 1 :b 2})
#{:a :b}
user=> (clojure.set/difference #{:a :b} {:a 1 :b 2 :c 3})
#{}

What I would have liked to happen in this case is for an exception to be 
thrown, or for the second argument to be implicitly converted to a set. An 
exception is thrown if the first element is not a set, so perhaps we should 
also have that behaviour in the case of the second.

There is inconsistency for at least the union function in the face of non 
set arguments as well:
user=> (clojure.set/union #{1} [1])
#{1}
user=> (clojure.set/union #{1} [1 2])
[1 2 1]

I can't think of any case where the current behaviour would be desirable, 
the most relevant post I can find on the issue quoted Rich Hickey as saying 
that the behaviour of the functions on non sets should not be relied 
upon<https://groups.google.com/d/msg/clojure/lQIP6-qJqWQ/8dFmHeXjI_gJ>. 
Another post<https://groups.google.com/d/msg/clojure/7QVFwtXWAi8/bhtkyXUWqxYJ>I 
read suggested (weakly) that someone had run across this behaviour and 
not found it as perverse as me.

Can anyone else think of a reason why we should not add type hints to the 
functions, or why coercing the arguments to sets is better (or something 
else I haven't thought of)?

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to