On Tue, Aug 26, 2008 at 11:54 PM, Shawn Hoover <[EMAIL PROTECTED]> wrote: > For one thing, it seems like there must be a cleaner way to handle > my use of reduce in test-shuffle-uniformity. I like reduce for > looping functionally, but in this case I just need to run n times > regardless of the specific values in (range n). Is that use case > common enough for some kind of reduce-times macro?
I can't say I've needed such a thing very often. Your own reduce-times macro might clarify your code a bit, though. > (defn test-shuffle-uniformity > "Shuffles a 3-item collection n times, mapping each result to the number of > times it's hit. If the distribution isn't uniform we've let bias into the > random numbers or the algorithm. Typical results on my machine: > (2 1 3) : 16611 > (3 2 1) : 16771 > (1 3 2) : 16707 > (1 2 3) : 16766 > (3 1 2) : 16555 > (2 3 1) : 16590" > ([n] (test-shuffle-uniformity n shuffle)) > ([n shuffle] > (let [coll [1 2 3] > results (reduce (fn [results _] > (let [result (shuffle coll)] > (assoc results result > (inc (or (get results result) > 0))))) > {} (range n))] > (doseq r results > (println (key r) ":" (val r)))))) You could simplify your reduce fn a bit by using merge-with: (defn test-shuffle-uniformity "Shuffles a 3-item collection n times, mapping each result to the number of times it's hit. If the distribution isn't uniform we've let bias into the random numbers or the algorithm. Typical results on my machine: (2 1 3) : 16611 (3 2 1) : 16771 (1 3 2) : 16707 (1 2 3) : 16766 (3 1 2) : 16555 (2 3 1) : 16590" ([n] (test-shuffle-uniformity n shuffle)) ([n shuffle] (let [coll [1 2 3] results (reduce (fn [results _] (merge-with + results {(shuffle coll) 1})) {} (range n))] (doseq r results (println (key r) ":" (val r)))))) --Chouser --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---