On Nov 21, 10:38 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > One thing that strikes me is that the (if (< num sample-size) ...) > test is unnecessary after sample-size has been > reached. It might be better to write it as two loops for that reason. >
I went ahead and made this too "func"ky by half, but at least it does no unnecessary comparisons. (defn random-sample [sample-size items] (let [[part-one part-two] (split-at sample-size items)] (second (reduce (fn [[num current] item] (let [index (rand-int num)] (if (< index sample-size) [(inc num) (assoc current index item)] [(inc num) current]))) [sample-size (vec part-one)] part-two)))) -- Steve --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---