Personally I think nth is broken and should throw an exception when passed
nil (which should be interpreted as an empty sequence)
=> (nth nil 10)
nil
=> (nth '() 10)
IndexOutOfBoundsException clojure.lang.RT.nthFrom (RT.java:885)
=> (nth [] 10)
IndexOutOfBoundsException clojure.lang.Persisten
I think Mailbox app might have messed with the quoting in my last reply. I hope
it makes sense.
I'm not suggesting that the function be changed - Clojure's API compatibility
between versions is amazing and a fine example of great engineering.
Reading other people's input on this thread
I also think it makes perfect sense for rand-nth to throw an error on an empty
collection. That's because the first step is it needs to generate a random
number between 0 and the length of the collection (0), which is impossible. So
it should throw an error. Note that it is the *random genera
Throwing an exception is probably the right thing to do if it's supposed to
closely model the behavior of `nth`, but it wonder if it's the most usefull
behavior in practice.
I would guess that it introduces `not-empty` checks everywhere, instead of only
for the cases where you want to be shure t
On Wed, Sep 30, 2015 at 12:14 PM, Mark Engelberg
wrote:
> I also think it makes perfect sense for rand-nth to throw an error on an
> empty collection. That's because the first step is it needs to generate a
> random number between 0 and the length of the collection (0), which is
> impossible. S
To me, the current behavior of nth makes sense because I know that the way
you traditionally write nth on a seq / linked list is to call rest n times
and then call first. Since Clojure's rest and first on nil (or the empty
list) return nil, then I would expect nth to return nil when called on an
e
Just for kicks, I built with nth throwing an exception on nil instead of
returning nil and all tests passed, so that's at least promising that
changing nth on nil would be possible.
On Wednesday, September 30, 2015 at 9:14:22 AM UTC-5, Alex Miller wrote:
>
> rand-nth cannot return a random eleme
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
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
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 t