On 10 January 2014 14:59, Colin Yates <[email protected]> wrote:
> I have a sequence of file names and I want to make them unique. (uniquify
> ["a" "b" "c" "a"]) => ["a" "b" "c" "a_1"])
>
> This is what I have come up with, but surely there is a better way?
>
>
I would do something like:
(defn uniquify
([xs]
(uniquify xs (fn [x n] (str x "_" n))))
([xs formatter]
(letfn [(uniquify* [xs seen]
(lazy-seq
(when (not-empty xs)
(let [x (first xs)
n (seen x 0)]
(if (> n 0)
(cons (formatter x n) (uniquify* (rest xs) (assoc
seen x (inc n))))
(cons x (uniquify* (rest xs) (assoc seen x (inc
n)))))))))]
(uniquify* xs {}))))
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.