dimanche 1 novembre 2015

In Clojure, why is this use of rand-int so complex?

Brief: Why use (reduce + (conj [] (repeat 1 (rand-int 100000)))) instead of simply (list (rand-int 100000)) when they seem to return equivalent results, namely, a list of a single pseudorandomly chosen integer?

Detailed: The longer version of the random int function is from the book Living Clojure by Carin Meyer, in her chapter on core.async, and is to be used to simulate the time servers take to respond.

... we will simulate calling them [two different servers] and having it take a random amount of time. This random amount of time will be a function called random-add. It uses a rand-int function that picks a random integer between 0 and 100,000. Then it uses it in a function that will sum up a vector filled with the number 1 of a random length:

(defn random-add [] (reduce + (conj [] (repeat 1 (rand-int 100000)))))

[emphasis added]

I'm a beginner, and really don't see what the advantage is over, for example:

(defn random-add-lite [] (list (rand-int 100000)))

In my repl the results are, as far as I can tell, equivalent. What's the difference and how does it help?

Thanks!




Aucun commentaire:

Enregistrer un commentaire