On Sun, Aug 8, 2010 at 12:12 AM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Just for fun another low-level solution:
>
> (defn partition-when
>  [pred coll]
>  (let [step (fn [p s]
>               (if s
>                 (let [fst (first s)]
>                   (if (pred fst)
>                     [p s]
>                     (recur (conj p fst) (next s))))
>                 [p nil]))]
>    (lazy-seq
>      (when-let [s (seq coll)]
>        (let [[p r] (step [(first s)] (next s))]
>          (cons p (partition-when pred r)))))))
>
> Hopefully not holding onto the head, as lazy as it can get and working with 
> infinite input.

I wrote a similar version in F# which does have the advantage of
handling infinite input or a very long partition in the sense that I
can still consume the first element immediately and can skip to the
second, third group ... In a sense, the result is a lazy list of lazy
list and if the consumer doesn't hold on to any of them, the memory
usage is constant. It is slower than a non-lazy version in F# when the
input is not too long in size.

I don't know about the characteristic of the current partition-by in
core, as this version would be a nice addition for its capability of
handling long input effectively.

-- 
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

Reply via email to