Hello Sir, (def *valid-chars* (vec "abcdefghijklmnopqrstuvwxuz0123456789" ))
This looks cleaner. Regards, Emeka On Sat, Oct 31, 2009 at 1:15 PM, John Harrop <jharrop...@gmail.com> wrote: > On Fri, Oct 30, 2009 at 7:47 PM, DavidF <davidnfind...@gmail.com> wrote: > >> >> Try this: >> >> (def *valid-chars* [ \a \b \c \d \e \f \g \h \i \j \k \l \m >> \n \o \p \q \r \s \t \u \v \w \x \u \z >> \0 \1 \2 \3 \4 \5 \6 \7 \8 \9 ] ) >> >> (defn generate-key [keylength] >> (for [x (range keylength)] (nth *valid-chars* (rand-int (count >> *valid- >> chars*))))) > > > There's a perfectly good reusable function hidden in there: > > (defn rand-elt [coll] > (nth coll (rand-int (count coll)))) > > (defn generate-key [key-length] > (for [x (range key-length)] (rand-elt *valid-chars*))) > > And you might want to wrap that for loop in (apply str ~the-loop). It also > might be more idiomatic to call *valid-chars* +valid-chars+ if it's not > normally going to be rebound or otherwise changed. > > (While we're on the topic of valid chars, how come the reader page > http://clojure.org/reader at clojure.org doesn't list as legal identifier > characters some characters that occur in identifiers in clojure.core? > Specifically, =, >, and <. > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---