2014/1/10 Stefan Kanev <stefan.ka...@gmail.com> > On 10/01/14, Colin Yates 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? > > > > 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))) > > I came up with the following version: > > (defn uniquify [words] > (loop [encountered {} > result [] > remaining words] > (if (seq remaining) > (let [word (first remaining) > occurences (get encountered word) > modified (if occurences > (str word "_" occurences) > word)] > (recur (update-in encountered [word] (fnil inc 0)) > (conj result modified) > (rest remaining))) > result))) > > It is a bit Scheme-ish. It builds a map of number of occurences as it > builds a vector, containing the result. It uses the map to figure out > whether to add a suffix or not. It preserves the original order of the > names. The variable names could use some love, but I don't have the > time for it now. > > If you want a lazy version, some modification is needed. >
Note that the lazy version would preserve doing too much computation ahead of time, but would not be any more space efficient than a non lazy version, even if the consumer of the lazy sequence does not retain the head. That is because at the same time that the remaining seq is consumed, the encountered map is updated. In the worst case scenario where all coll items are different, the map when then last lazy seq item is produced will be of the same size as the initial sequence. > -- > Stefan Kanev Ś @skanev Ś http://skanev.com/ > Giving up on assembly language was the apple in our Garden of Eden: > Languages > whose use squanders machine cycles are sinful. The LISP machine now > permits > LISP programmers to abandon bra and fig-leaf. > > -- > -- > 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.