Hi Jarek, Have a look at the clojure documentation: nth Returns the value at the index. get returns nil if index out of bounds, nth throws an exception unless not-found is supplied. nth also works for strings, Java arrays, regex Matchers and Lists, and, in O(n) time, for sequences.
Unfortunately, you can't provide not-found for rand-nth, which probably is a bit inconsistent: (nth [] 1) => OutOfBoundsException (nth [] 1 0) => 0 (rand-nth [] 0) ;; Doesn't exist On 13/06/2011, at 6:34 AM, jarek wrote: > What am I missing here? Why is rand-nth throwing the oob exception > instead of returning nil as I'd expect by analogy to other routines. > > $ clojure > Clojure 1.2.0 > user=> (rand-nth [1 2 3]) > 2 > user=> (rand-nth []) > java.lang.IndexOutOfBoundsException (NO_SOURCE_FILE:0) > user=> (get [1 2 3] 5) > nil > user=> (get [] 0) > nil > > > regards, > > jarek > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- "Test-driven Dentistry (TDD!) - Not everything should be test driven" - Michael Fogus -- ********************************************************** Andreas Koestler, Software Engineer Leica Geosystems Pty Ltd 270 Gladstone Road, Dutton Park QLD 4102 Main: +61 7 3891 9772 Direct: +61 7 3117 8808 Fax: +61 7 3891 9336 Email: [email protected] ************www.leica-geosystems.com************* when it has to be right, Leica Geosystems Please consider the environment before printing this email. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
