Re: equivalent to Haskell's group function

2009-11-09 Thread Meikel Brandmeyer
Hi, Am 09.11.2009 um 07:33 schrieb Wilson MacGyver: I did search in the API docs for both core and contrib, but didn't find anything like it. Does Clojure have a function like Haskell's group? In Haskell, Input: group [1,2,2,1,1,1,2,2,2,1] Output: [[1],[2,2],[1,1,1],[2,2,2],[1]] I'm

Re: equivalent to Haskell's group function

2009-11-09 Thread Emeka
Meikel, Is like you over engineered your version? (defn group [s] (lazy-seq (when-let [s (seq s)] (let [f(first s) [fs r] (split-with #(= % f) s)] (cons fs (group r)) Should be .. (defn group [s] (lazy-seq (when-let [s (seq s)] (let [f

Re: equivalent to Haskell's group function

2009-11-09 Thread Emeka
Meikel, What is the gain of using lazy-seq here? Why can't we go without laziness? (defn group [s] (when-let [s (seq s)] (let [f(first s) [fs r] (split-with #(= % f) s)] (cons fs (group r) Regards, Emeka On Mon, Nov 9, 2009 at 4:44 PM, Emeka

Re: equivalent to Haskell's group function

2009-11-09 Thread David Brown
On Mon, Nov 09, 2009 at 04:49:16PM +, Emeka wrote: What is the gain of using lazy-seq here? Why can't we go without laziness? - The lazy version doesn't consume stack per length of the sequence. - The lazy version works with unbounded sequences. For short sequences it probably

Re: equivalent to Haskell's group function

2009-11-09 Thread Wilson MacGyver
Thanks guys for the various solutions. I set out trying to try a recur solution So I came up with this. the idea is to go through the collection being passed, and grab one element, then do drop-while until a different element is encountered. repeat until there is no more left in the collection.

Re: equivalent to Haskell's group function

2009-11-09 Thread Emeka
(defn group [x] (loop [newlist [] currlist x] (if (not (empty? x)) (recur (newlist (cons (first x) newlist)) (newlist (cons (first x) newlist)) You are making a function call here using an empty vector and your argument is a list. This is not possible, that's why you have that error ([] (cons

Re: equivalent to Haskell's group function

2009-11-09 Thread Wilson MacGyver
On Mon, Nov 9, 2009 at 2:31 PM, Emeka emekami...@gmail.com wrote: (defn group [x] (loop [newlist [] currlist x]  (if (not (empty? x))    (recur (newlist (cons (first x) newlist)) (newlist (cons (first x) newlist))  You are making a function call here using an empty vector and your argument

Re: equivalent to Haskell's group function

2009-11-09 Thread Meikel Brandmeyer
Hi, Am 09.11.2009 um 20:08 schrieb Wilson MacGyver: (defn group [x] (loop [newlist [] currlist x] (if (not (empty? x)) (recur (newlist (cons (first x) newlist)) (currlist (drop-while #(= (first currlist) %) currlist)) It seems logical to me, but when I tried

Re: equivalent to Haskell's group function

2009-11-09 Thread Wilson MacGyver
Ah that was what I was missing. You are right, I misunderstood the recur form. For some reason I thought in recur you have to specify the name of the variable that it's bound to. so when I wrote (recur (newlist (cons (first x) newlist)) I thought I was saying in recur, rebind (cons (first x)

Re: equivalent to Haskell's group function

2009-11-08 Thread Alex Osborne
Wilson MacGyver wrote: Does Clojure have a function like Haskell's group? In Haskell, Input: group [1,2,2,1,1,1,2,2,2,1] Output: [[1],[2,2],[1,1,1],[2,2,2],[1]] (use 'clojure.contrib.seq-utils) (partition-by identity [1 2 2 1 1 1 2 2 2 1]) = ((1) (2 2) (1 1 1) (2 2 2) (1))

Re: equivalent to Haskell's group function

2009-11-08 Thread Wilson MacGyver
ah, thank you for the help! On Mon, Nov 9, 2009 at 1:57 AM, Alex Osborne a...@meshy.org wrote: Wilson MacGyver wrote: Does Clojure have a function like Haskell's group? In Haskell, Input: group [1,2,2,1,1,1,2,2,2,1] Output: [[1],[2,2],[1,1,1],[2,2,2],[1]] (use