Here's a solution based on your description that behaves like core
partition fns, taking liberty to presume your example should've matched the
output below.
(defn partition-by-seq
[sub-seq coll]
(letfn [(step [coll]
(when-let [coll (seq coll)]
(let [[run more] (split-with (partial not= (first sub-seq)) coll)
[possible-match more] (split-at (count sub-seq) more)]
(if (= possible-match sub-seq)
(if (seq run)
(cons run (cons possible-match (lazy-seq (step more))))
(cons possible-match (lazy-seq (step more))))
(cons (concat run possible-match) (lazy-seq (step
more)))))))]
(or (step coll) ())))
=> (partition-by-seq [:b :c] [])
()
=> (partition-by-seq [:b :c] [:a :d])
((:a :d))
=> (partition-by-seq [:b :c] [:a :b :c :d :a :b :c :d :a :b :c])
((:a) (:b :c) (:d :a) (:b :c) (:d :a) (:b :c))
On Tuesday, June 3, 2014 4:05:16 AM UTC-5, Ulrich Küttler wrote:
>
> Hi,
>
> what is the preferred way to find sub-seqs in a seq? I am trying to convert
>
> [:a :b :c :d :a :b :c :d :a :b :c]
>
> into
>
> ((:a) (:b :c) (:a :d) (:b :c) (:a))
>
> using the sub-seq (:b :c) instead of positions.
>
> partition, partition-by and the like all look at one element at a time.
> What I need is a search based on seqs. Are there functions that support
> such a search / split?
>
> Uli
>
--
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.