On 11 January 2014 01:14, Alan Forrester <alanmichaelforres...@googlemail.com> wrote: > On 11 January 2014 01:03, Alan Forrester > <alanmichaelforres...@googlemail.com> wrote: >> On 10 January 2014 21:06, Colin Yates <colin.ya...@gmail.com> wrote: >>> Gosh - my public humiliation continues. Here is one that actually works: >>> >>> (first (reduce (fn [[results seen] item] >>> (let [cnt (get seen item 0)] >>> [(conj results (if (> cnt 0) (format-fn item cnt) >>> item)) >>> (assoc seen item (inc cnt))])) >>> [[] {}] >>> items)) >>> (fact "strings can be made unique" >>> (s/uniquify ["a" "b" "c"]) => ["a" "b" "c"] >>> (s/uniquify ["a" "b" "a" "c" "b" "b" "b" "b" "a"]) => ["a" "b" "a_1" "c" >>> "b_1" "b_2" "b_3" "b_4" "a_2"]) >> >> My first two suggestions produce unique titles. They don't do what you >> want but they are brief: >> (defn uniquify [a] (map #(str (gensym %)) a)) >> (defn uniquify [a] (map-indexed (fn [ind el] (str el "_" ind)) a)) >> >> My last suggestion does what you want >> (defn uniquify [a] >> (loop [res [(first a)] leftover (rest a)] >> (if-not (empty? leftover) >> (let [freqy (frequencies res) >> fl (first leftover) >> ffl (freqy fl)] >> (if ffl >> (recur (concat res [(str fl "_" ffl)]) (rest leftover)) >> (recur (concat res [fl]) (rest leftover)))) >> res))) > > Actually, it doesn't work.
This version works: (defn uniquify [a] (loop [res [(first a)] intermed [(first a)] leftover (rest a)] (if-not (empty? leftover) (let [freqy (frequencies intermed) fl (first leftover) ffl (freqy fl)] (if ffl (recur (concat res [(str fl "_" ffl)]) (concat intermed [fl]) (rest leftover)) (recur (concat res [fl]) (concat intermed [fl]) (rest leftover)))) res))) Alan -- -- 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/groups/opt_out.