Re: How can I improve this?

2014-01-11 Thread Mark Engelberg
Very clever! On Fri, Jan 10, 2014 at 8:10 PM, Håkan Råberg hakan.rab...@gmail.comwrote: Another style, using channels for local state, but could been plain old iterators, slight golf warning: (require '[clojure.core.async :refer [to-chan !!]]) (defn uniquify [s formatter] (let [g

Re: How can I improve this?

2014-01-11 Thread Alex Baranosky
I've been finding uses for Brandon Bloom's transduce mini-library left and right lately. There is a class of problems where you want to track some state as you process a seq, and transduce.lazy/map-state enables you to do that (https://github.com/brandonbloom/transduce). Here's my solution using

Re: How can I improve this?

2014-01-11 Thread Alex Osborne
On Sat, January 11, 2014 9:22 pm, Alex Baranosky wrote: There is a class of problems where you want to track some state as you process a seq This is an interesting exercise. I find said class of problem comes up fairly frequently and I usually end up with something scary-looking using reduce or

Re: How can I improve this?

2014-01-10 Thread Guru Devanla
Hi Colin, Clojure has a distinct function that does this. I may be missing some context on what you what out of your new method that 'distinct' does not provide. The distinct functions source code is a good reference. Thanks On Fri, Jan 10, 2014 at 6:59 AM, Colin Yates colin.ya...@gmail.com

Re: How can I improve this?

2014-01-10 Thread Alex Miller
I would not use an atom. Think about it as doing a reduce while passing along a set of the names you've seen so far. You might also look at the implementation of distinct in clojure.core which is similar (you want to detect duplicates in the same way, but emit new names instead of omitting

RE: How can I improve this?

2014-01-10 Thread Colin Yates
07:10:22 -0800 Subject: Re: How can I improve this? From: grd...@gmail.com To: clojure@googlegroups.com Hi Colin, Clojure has a distinct function that does this. I may be missing some context on what you what out of your new method that 'distinct' does not provide. The distinct functions source

Re: How can I improve this?

2014-01-10 Thread Colin Yates
Good call. I keep discounting reduce as I am not 'reducing' anything, only transforming (i.e. map) it - my mistake. Thanks. Col On Friday, 10 January 2014 15:12:27 UTC, Alex Miller wrote: I would not use an atom. Think about it as doing a reduce while passing along a set of the names

Re: How can I improve this?

2014-01-10 Thread Ray Miller
On 10 January 2014 14:59, Colin Yates colin.ya...@gmail.com 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]

Re: How can I improve this?

2014-01-10 Thread Laurent PETIT
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

Re: How can I improve this?

2014-01-10 Thread Colin Yates
Love it. Much more readable without any nasty persistent state. On Friday, 10 January 2014 15:19:03 UTC, Ray Miller wrote: On 10 January 2014 14:59, Colin Yates colin...@gmail.com javascript:wrote: I have a sequence of file names and I want to make them unique. (uniquify [a b c a]) = [a b

Re: How can I improve this?

2014-01-10 Thread Colin Yates
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

Re: How can I improve this?

2014-01-10 Thread Guru Devanla
... -- Date: Fri, 10 Jan 2014 07:10:22 -0800 Subject: Re: How can I improve this? From: grd...@gmail.com To: clojure@googlegroups.com Hi Colin, Clojure has a distinct function that does this. I may be missing some context on what you what out of your new method

Re: How can I improve this?

2014-01-10 Thread Jim - FooBar();
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 %

Re: How can I improve this?

2014-01-10 Thread Laurent PETIT
Le vendredi 10 janvier 2014, Colin Yates a écrit : 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,

Re: How can I improve this?

2014-01-10 Thread Jim - FooBar();
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

Re: How can I improve this?

2014-01-10 Thread Jim - FooBar();
oops! my fn will not keep the original ordering...sorry Colin Jim On 10/01/14 15:34, Jim - FooBar(); wrote: 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

Re: How can I improve this?

2014-01-10 Thread Stefan Kanev
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

Re: How can I improve this?

2014-01-10 Thread Laurent PETIT
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

Re: How can I improve this?

2014-01-10 Thread Jonas
Here's a version using reduce: (defn uniquify [items] (first (reduce (fn [[result count-map] item] (let [n (inc (count-map item 0))] [(conj result (str item _ n)) (assoc count-map item n)])) [[] {}]

Re: How can I improve this?

2014-01-10 Thread Stefan Kanev
Somehow I totally forgot I could use destructuring. Here's a slightly shorter version: (defn uniquify [words] (loop [encountered {} result [] [word remaining] words] (if (seq remaining) (let [occurences (get encountered word)

Re: How can I improve this?

2014-01-10 Thread Laurent PETIT
no you have a bug in this last version, it now skips the last result 2014/1/10 Stefan Kanev stefan.ka...@gmail.com Somehow I totally forgot I could use destructuring. Here's a slightly shorter version: (defn uniquify [words] (loop [encountered {} result []

Re: How can I improve this?

2014-01-10 Thread Laurent PETIT
What about this one? Inspired by Stefan's, with more destructuring in loop, format-fn as a function, initial call to (seq) then (next) instead of (rest), placing the exit argument first so that it's not lost at the end of the function, renamed word as item since this function does not depend on

Re: How can I improve this?

2014-01-10 Thread Stefan Kanev
On 10/01/14, Laurent PETIT wrote: What about this one? Inspired by Stefan's, with more destructuring in loop, format-fn as a function, initial call to (seq) then (next) instead of (rest), placing the exit argument first so that it's not lost at the end of the function, renamed word as item

Re: How can I improve this?

2014-01-10 Thread Mark Engelberg
If all you need is unqiueness, why not just number *all* the filenames in sequential order, something like: (defn uniqueify [filenames] (map (fn [filename number] (str filename \_ number)) filenames (iterate inc 1))) -- -- You received this message because you are subscribed to the Google

Re: How can I improve this?

2014-01-10 Thread Sean Corfield
java.jdbc does this for column names (in joins): https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc.clj#L257 Sean On Jan 10, 2014, at 6:59 AM, Colin Yates colin.ya...@gmail.com wrote: I have a sequence of file names and I want to make them unique. (uniquify

Re: How can I improve this?

2014-01-10 Thread Guru Devanla
But, for the given problem this may not be directly helpful. This method only returns the next unique name for the given list. The logic would have to traverse the list n times for n elements (and some factor of no of duplicates) to get the desired result, correct? Thanks Guru On Fri, Jan 10,

Re: How can I improve this?

2014-01-10 Thread Colin Yates
This and Jonas' are my current favourites, for what that's worth. Keep the suggestions coming! On Friday, 10 January 2014 17:29:20 UTC, Laurent PETIT wrote: What about this one? Inspired by Stefan's, with more destructuring in loop, format-fn as a function, initial call to (seq) then

Re: How can I improve this?

2014-01-10 Thread Guru Devanla
Actually, you might have meant line 267? On Fri, Jan 10, 2014 at 11:03 AM, Sean Corfield s...@corfield.org wrote: java.jdbc does this for column names (in joins): https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc.clj#L257 Sean On Jan 10, 2014, at 6:59

Re: How can I improve this?

2014-01-10 Thread Mark Engelberg
Technically, all these solutions are flawed. With the input [a a a_1] you'll get back [a a_1 a_1] To truly address this, you need to also add the newly formatted filename into the seen map, which none of the suggested solutions do. -- -- You received this message because you are subscribed to

Re: How can I improve this?

2014-01-10 Thread Jonas Enlund
On Fri, Jan 10, 2014 at 9:39 PM, Mark Engelberg mark.engelb...@gmail.comwrote: Technically, all these solutions are flawed. With the input [a a a_1] you'll get back [a a_1 a_1] To truly address this, you need to also add the newly formatted filename into the seen map, which none of the

Re: How can I improve this?

2014-01-10 Thread Mark Engelberg
On Fri, Jan 10, 2014 at 11:43 AM, Jonas Enlund jonas.enl...@gmail.comwrote: That's why I wrote my solution like I did, i.e., concatenate _1 when a new string is found. This would result in the vector [a_1 a_2 a_1_1] Right, I agree that works, as does my tack unique numbers onto the end of

Re: How can I improve this?

2014-01-10 Thread Colin Yates
way to take the wind out of our sails! Well spotted :). On Friday, 10 January 2014 19:39:45 UTC, puzzler wrote: Technically, all these solutions are flawed. With the input [a a a_1] you'll get back [a a_1 a_1] To truly address this, you need to also add the newly formatted filename

Re: How can I improve this?

2014-01-10 Thread Cedric Greevey
On Fri, Jan 10, 2014 at 10:22 AM, Laurent PETIT laurent.pe...@gmail.comwrote: 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 uPhine? :) -- -- You received this message because

Re: How can I improve this?

2014-01-10 Thread Mark Engelberg
On Fri, Jan 10, 2014 at 11:52 AM, Colin Yates colin.ya...@gmail.com wrote: way to take the wind out of our sails! Well spotted :). It's not too hard to fix. Here's an adapted version of Jonas' solution that should do the trick: (defn uniqueify [items] (first (reduce (fn [[results

Re: How can I improve this?

2014-01-10 Thread Colin Yates
I thought I would have a go myself without copying (although having read them earlier) the other functions and this is what I came up with: (first (reduce (fn [[results seen] item] (let [occurrences ((fnil identity 0) (get seen item)) seen (assoc

Re: How can I improve this?

2014-01-10 Thread Mark Engelberg
On Fri, Jan 10, 2014 at 12:55 PM, Colin Yates colin.ya...@gmail.com wrote: Being really anal I could claim the original a_2 should remain a_2 and the third instance of a jump to being a_3. Sure, but that would require two passes. Otherwise, there's no way when you encounter the third a to

Re: How can I improve this?

2014-01-10 Thread Colin Yates
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

Re: How can I improve this?

2014-01-10 Thread Laurent PETIT
okay, new take solving the issue raised by Mark: (defn uniquify [in format-fn] (loop [[item :as in] (seq in) {n item :as item-nbrs} {} out []] (if-not in out (let [format-fn (if n format-fn (constantly item)) new-item (format-fn item n)]

Re: How can I improve this?

2014-01-10 Thread Mark Engelberg
Laurent, your approach doesn't quite work: = (uniquify [a_1 a a] (fn [s n] (str s \_ n))) [a_1 a a_1] On Fri, Jan 10, 2014 at 1:34 PM, Laurent PETIT laurent.pe...@gmail.comwrote: okay, new take solving the issue raised by Mark: (defn uniquify [in format-fn] (loop [[item :as in] (seq in)

Re: How can I improve this?

2014-01-10 Thread Laurent PETIT
Indeed, I should definitely recur as you do Le vendredi 10 janvier 2014, Mark Engelberg a écrit : Laurent, your approach doesn't quite work: = (uniquify [a_1 a a] (fn [s n] (str s \_ n))) [a_1 a a_1] On Fri, Jan 10, 2014 at 1:34 PM, Laurent PETIT laurent.pe...@gmail.comwrote: okay, new

Re: How can I improve this?

2014-01-10 Thread Ralf Schmitt
Colin Yates colin.ya...@gmail.com writes: This and Jonas' are my current favourites, for what that's worth. Keep the suggestions coming! here's another one that uses reductions. formatter would need to be adapted a bit, since it currently also adds _0 for the first name seen: (defn

Re: How can I improve this?

2014-01-10 Thread Sean Corfield
I meant the code that starts at line 257 (and continues to line 274): two functions, the second one calls the first one. Luckily, java.jdbc's code seems to pass all the test cases posted to this thread so far (arguably more intuitively, the second occurrence gets _2 appended, the third _3

Re: How can I improve this?

2014-01-10 Thread Alan Forrester
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)

Re: How can I improve this?

2014-01-10 Thread Alan Forrester
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

Re: How can I improve this?

2014-01-10 Thread Alan Forrester
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

Re: How can I improve this?

2014-01-10 Thread Håkan Råberg
Another style, using channels for local state, but could been plain old iterators, slight golf warning: (require '[clojure.core.async :refer [to-chan !!]]) (defn uniquify [s formatter] (let [g (memoize #(to-chan (cons % (map (partial formatter %) (next (range))] (map (fn f [x]