On Jan 27, 8:21 pm, Scott <sbuck...@gmail.com> wrote: > wondering if I can please get some advice on how to improve the > performance of this piece of code > > (defn select-n-tournament > [popu fit-fn n] > (let [k 7] > (take n (repeatedly #(first (sort-by fit-fn > (take k (shuffle > popu)))))) > ) > )
I don't know about using map, but here is a function inspired by one called 'most' in Paul Graham's On Lisp. You could use (most fit-fn (take k (shuffle popu))) in place of your (first ...) subexpression above, and it would avoid sorting elements that you would otherwise simply be throwing away anyway. (defn most [fit-fn c] "Return the element e of collection c that has the largest numerical value of (fit-fn e)." (let [c (seq c)] (when c (loop [winner (first c) winner-fitness (fit-fn winner) c (rest c)] (if (seq c) (let [e (first c) e-fitness (fit-fn e)] (if (> e-fitness winner-fitness) (recur e e-fitness (rest c)) (recur winner winner-fitness (rest c)))) winner))))) Andy -- 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