Hi Frank,

I use a similar function that combines partition-by and partition-all:

(defn partition-when
  [f coll]
  (map (partial apply concat) (partition-all 2 (partition-by f coll))))

Martin.


On Wed, Mar 4, 2015 at 10:19 PM, Ivan L <ivan.laza...@gmail.com> wrote:

> I went though almost the exact same exercise and my code is almost
> identical to yours.  I called it partition-every and I use it a lot.  A
> more determined individual might submit this for inclusion into core!
>
>
> On Tuesday, March 3, 2015 at 2:46:29 PM UTC-5, Frank wrote:
>>
>> Hi all,
>>
>> for some tests I need a function which starts a new partition each time a
>> predicate returns true, for instance:
>>
>> (partition-when
>>   (fn [s] (.startsWith s ">>"))
>>   [">> 1" "2" "3" ">> 4" "5" "6"])
>> :=> [[">> 1" "2" "3"] [">> 4" "5" "6"]]
>>
>> Since I haven't found a built-in function, I copied, pasted, and modified
>> the core function partition-by:
>>
>> (defn partition-when
>>   [f coll]
>>   (lazy-seq
>>    (when-let [s (seq coll)]
>>      (let [fst (first s)
>>            run (cons fst (take-while #(not (f %)) (next s)))]
>>        (cons run (partition-when f (seq (drop (count run) s))))))))
>>
>> Is there a better (more idiomatic) way to achieve the same result?
>>
>> Thank you in advance.
>>
>> Frank
>>
>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 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 clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to