Here's a version with reduce. It returns your elements as sets, but you could
easily (map seq ,,,) if you really need lists.
user=> (reduce (fn [r e] (if ((last r) e) (conj r #{e}) (update-in r [(dec
(count r))] conj e))) [#{}] [1 2 2 3 4 4 1 6])
[#{1 2} #{2 3 4} #{1 4 6}]
Cheers, Jay
On Mar 30, 2012, at 4:29 AM, David Jagoe wrote:
> On 29 March 2012 21:41, Cedric Greevey <[email protected]> wrote:
>>
>> On Thu, Mar 29, 2012 at 4:18 PM, David Jagoe <[email protected]> wrote:
>
>>> Given a sequence like this: [1 2 1 2 1 1 2 1 2 2 2]
>>>
>>> partition it to get this: [(1 2) (1 2) (1) (1 2) (1 2) (2) (2)]
>>> !
>>
>> (defn partition-distinct [s]
>> (lazy-seq
>> (loop [seen #{} s (seq s) part []]
>> (if s
>> (let [[f & r] s]
>> (if (seen f)
>> (cons (seq part) (partition-distinct s))
>> (recur (conj seen f) r (conj part f))))
>> (if-let [part (seq part)]
>> (list part))))))
>>
>
> Thanks! I had a nasty feeling that it could be done in a one-liner
> using partition-by or something similar. But of course you need to be
> able to look at previous elements...
>
> --
> 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 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