On Thu, Mar 29, 2012 at 4:18 PM, David Jagoe <[email protected]> wrote:
> Hi all,
>
> I'm sure I'm missing a really simple way of doing this!
>
> 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)]
>
> I've been trying to write something generic like partition-by because the
> values are maps. Also I want to be able to deal with more than 2 distinct
> values.
>
> Thanks!
(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))))))
--
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