Updated the gist to fix a case '(:a :b) should've returned '((:a :b)) to
match partition-by.
New: The sub-seq can optionally contain fns, just like partition-by's f
param.
;; Case: using a seq of fns instead of values
(= (partition-by-seq [odd? odd? even?] [1 2 3 5 6 7 8 9 11 12])
'((1 2) (
This is sweet. Thanks a lot Linus and Alex. So there is no function to do
it, you just have to build it yourself. I was not sure of that.
I like Alex's solution. This is something I can read and understand. Also,
it would be nice to compare and split in one loop. This is what I came up
with, based
https://gist.github.com/alexpw/f20c7b3ac858003e07e2
This version supports the missing case:
;; Case: false match :b, followed by true match :b :c
(= (partition-by-seq [:b :c] [:a :b :b :c :e :d :a :b :c :d :a :b :c])
'((:a :b) (:b :c) (:e :d :a) (:b :c) (:d :a) (:b :c)))
On Sunday, June 8, 2
Err, this was bugging me all day when I went afk. I wrote it too quickly
and am missing the case where the sub-seq starts right after seeing the
first val, [:b :b :c].
Will fix it if I have time today, but may need to take a slightly different
approach. Fun problem, btw. :)
On Saturday, June
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] (spli
Maybe reduce-fsm could be useful?
https://github.com/cdorrat/reduce-fsm
It creates a simple state finite state machine that can be applied on any
sequence.
/Linus
2014-06-03 11:04 GMT+02:00 Ulrich Küttler :
> Hi,
>
> what is the preferred way to find sub-seqs in a seq? I am trying to convert