Re: Exception calling nth on Sets

2011-08-15 Thread Meikel Brandmeyer (kotarak)
Hi,

Am Sonntag, 14. August 2011 18:59:57 UTC+2 schrieb Despite:

 In Clojurescript, calling rand-nth on an empty Set results in an 
 Index out of bounds exception, whereas calling it on an empty Vector 
 results in nil. 


Returning nil for vectors is almost certainly a bug as it should throw an 
exception according to the docstring of nth.

Sincerely
Meikel

-- 
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

Re: Exception calling nth on Sets

2011-08-15 Thread Despite
I should have done a comparison to original Clojure first.

In Clojure, rand-nth on an empty vector does throw an exception, as
Meikel says.  As does any use of nth with a bad index and no not-found
parameter.  So that seems to be a bug.

But, nth on a set never works in Clojure, throwing
java.lang.UnsupportedOperationException: nth not supported on this
type: PersistentHashSet.  So, the fact that it works at all in
Clojurescript seems to be a bug as well.


On Aug 15, 1:13 am, Meikel Brandmeyer (kotarak) m...@kotka.de
wrote:
 Hi,

 Am Sonntag, 14. August 2011 18:59:57 UTC+2 schrieb Despite:



  In Clojurescript, calling rand-nth on an empty Set results in an
  Index out of bounds exception, whereas calling it on an empty Vector
  results in nil.

 Returning nil for vectors is almost certainly a bug as it should throw an
 exception according to the docstring of nth.

 Sincerely
 Meikel

-- 
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


Exception calling nth on Sets

2011-08-14 Thread Despite
In Clojurescript, calling rand-nth on an empty Set results in an
Index out of bounds exception, whereas calling it on an empty Vector
results in nil.

ClojureScript:cljs.user (rand-nth (set []))
Error evaluating: (cljs.core.prn (rand-nth (set []))) :as
cljs.core.prn.call(null,cljs.core.rand_nth.call(null,cljs.core.set.call(null,cljs.core.Vector.fromArray([];
\nsun.org.mozilla.javascript.internal.JavaScriptException: Index out
of bounds (cljs/core.cljs#937)

ClojureScript:cljs.user (rand-nth (vec []))
nil

Drilling down, the problem is that calling nth with an out-of-range
index returns nil for Vectors, but throws that exception for Sets.
But, using the set as a function, you do get nil:

ClojureScript:cljs.user ((set [1 2 3]) 10)
nil

Am I doing something wrong?  Or is there a bug in how nth is defined
for Sets?

-- 
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


Re: Exception calling nth on Sets

2011-08-14 Thread Mark Engelberg
My guess is that nth (and by extension rand-nth) can't implicitly call
seq on the collection because if it did, you'd lose the fast access
that vectors provide.  So you'll need to call seq on your set before
passing it to rand-nth.

-- 
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