2017-09-06 9:58 GMT+02:00 Cecil Westerhof <cldwester...@gmail.com>: > I want to start using Clojure again. I made the following simple function > to generate a PIN: > (defn create-pin > ([] (create-pin 8)) > ([n] > (let [chars (map char (range (int \0) (inc (int \9))))] > (reduce str (repeatedly n #(rand-nth chars)))))) > > So far so good. But I want to improve a little. > > I think n should at least be four, but not greater as 16. What is the > Clojure way to do this? > > The next step is that I want to use hexadecimal numbers. So I should use > (range (int \0) (inc (int \9))) combined with (range (int \A) (inc (int > \F))). > How would I do that? > > Is there anything I should do differently? > > Of-course I make a general function that is then called from create-pin > and create-pin-hex. >
With the help of this list I rewrote it to: (def digits (apply str (map char (range (int \0) (inc (int \9)))))) (def hex-digits (apply str digits (map char (range (int \A) (inc (int \F)))))) (defn create-pin ([] (create-pin 8)) ([n] {:pre [(<= n 16) (>= n 4)]} (reduce str (repeatedly n #(rand-nth digits))))) (defn create-pin-hex ([] (create-pin-hex 8)) ([n] {:pre [(<= n 16) (>= n 4)]} (reduce str (repeatedly n #(rand-nth hex-digits))))) Indention is not great: I have to find out how to modify emacs for it. By the way does has clojure something like super? Using: (defn create-pin-hex ([] (create-pin-hex 8)) is asking for trouble. After copy/pasting from create-pin I almost forgot to change it. -- Cecil Westerhof -- 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.