actually `post-fn` should be #(group-by identity (-> % meta :encountered))
Jim
On 10/01/14 15:28, Jim - FooBar(); wrote:
I quickly put together this which seems to preserver the orderof the
original seq:
(defn uniquify [coll]
(let [post-fn #(group-by first (-> % meta :encountered))]
(loop [unique (with-meta [] {:encountered []})
[f & more] coll]
(if (nil? f) (flatten (concat unique (reduce #(conj % (map str
(second %2) (range))) [] (post-fn unique))))
(recur (if-not (some #{f} unique) (conj unique f) (vary-meta
unique update-in [:encountered] conj f)) more)))))
HTH,
Jim
On 10/01/14 15:24, Colin Yates wrote:
I did consider that but I want to preserve the order of the incoming
sequence.
On Friday, 10 January 2014 15:22:29 UTC, Laurent PETIT wrote:
Hi,
Use frequencies to get a map of path => nb of occurrences, then
for each entry of the map, create unique names.
Cannot provide an impl on the uPhine, sorry
Le vendredi 10 janvier 2014, Colin Yates a écrit :
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?
What would you all do? Feedback welcome (including the word
'muppet' as I am sure I have missed something simple) :)
(defn uniquify
"Return a sequence, in the same order as s containing every
element
of s. If s (which is presumed to be a string) occurs more
than once
then every subsequent occurrence will be made unique.
Items will be updated to include an incrementing numeric
count using
the specified formatter function. The formatter function
will be
given the name and the number and should return a
combination of the
two.
The set of unique s's in the returned sequence will be the
count of
s's in s."
([s] (uniquify s (fn [item duplicates] (str item "_"
duplicates))))
([s formatter]
(let [occurrences (atom {})
register-occurrence (fn [item]
(if (get @occurrences item)
(swap! (get @occurrences
item) inc)
(swap! occurrences assoc
item (atom 1)))
@(get @occurrences item))
process (fn [item]
(let [duplicates (dec
(register-occurrence item))]
(if (> duplicates 0)
(formatter item duplicates)
item)))
unique-s (map process s)]
unique-s)))
--
--
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
<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
<https://groups.google.com/groups/opt_out>.
--
--
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.
--
--
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.