rand-nth cannot return a random element if there are no elements to return, 
so I do not find this surprising. Returning nil would be an invalid answer 
in my opinion.

The rand-nth doc string implies that rand-nth will inherit the semantics of 
nth for the specified collection. nth will generally throw on an invalid 
index (and all indexes are invalid for an empty collection).

For nil, the docstring for nth does not mention nil explicitly, so I would 
treat this as undefined behavior unless you consider it as a degenerate 
sequence. 

The code (in RT.nthFrom()) explicitly handles this case and effectively 
treats nil as an infinite indexed source of nils, rather than as an empty 
sequence. In my own opinion (but maybe not Rich's), it seems to make more 
sense to take the sequence interpretation and say that on nil, nth should 
throw instead.

So that might be a valid ticket. I'm not sure what, if any, negative 
consequences would arise out of existing uses of nth that rely on this 
behavior though. Given the number of things built on nth, it may be 
impossible to change this even if it made sense to.

Alex
 
On Wednesday, September 30, 2015 at 6:38:49 AM UTC-5, Marc O'Morain wrote:
>
> Hi all,
>
> I was surprised by the fact that rand-nth thrown when called on an empty 
> collection - see http://dev.clojure.org/jira/browse/CLJ-925 
> <http://www.google.com/url?q=http%3A%2F%2Fdev.clojure.org%2Fjira%2Fbrowse%2FCLJ-925&sa=D&sntz=1&usg=AFQjCNFpBhBI2wA5LnBRCiAP8oYyF5HdOg>
>
> This behaviour is strange, since passing nil to rand-nth returns nil, 
> whereas in my experience, other Clojure functions treat nil and empty 
> collections in the same way (compare with next/rest/first, etc).
>
> http://dev.clojure.org/jira/browse/CLJ-925 was marked as Completed 
> without comment – is this the intended behaviour? If so, would you accept a 
> patch to update the docstring to make it clear that the function will throw 
> an exception when passed an empty collection?
>
> Thanks,
>
> Marc
>

-- 
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/d/optout.

Reply via email to