*Warning this message contains mutable state and may hurt functional sensibilities.*
Ugly hack: (defn my-split-with [pred coll] (let [s (atom coll) p #(when-let [r (pred %)] (swap! s rest) r)] [(take-while p coll) (drop-while pred (lazy-seq @s))])) Now it works ;-) Laurent PETIT a écrit : > This is a general problem with function (split-with) (and derivatives > such as partition-by ...), > > This should certainly deserve a mention in their respective > docstrings, I think. Because the docstring speak about lazyness, but > not the kind of lazyness that can avoid Out of Memory in corner cases. > > Rich, if you agree with that, would you me to issue a patch on google group ? > > 2009/4/23 Christophe Grand <christo...@cgrand.net>: > >> Laurent PETIT a écrit : >> >>> Hi Meikel, >>> >>> It seems to me that your version is the only safe one so far, that >>> would succesfully indefinitely return values with this test: >>> >>> (dorun (mystery-function true? :foo (repeat true))) >>> >>> Mine, a new version of mine I'll never bother to publish, and >>> Christophe's all retain head. >>> To explain on Christophe's one for example: >>> >>> It uses (split-with) which, in case pred always match coll elements, >>> will retain the head of coll in etc, while eating more and more >>> elements of coll via running on run : >>> 1:21 user=> (defn mystery-function [pred coll] >>> (lazy-seq >>> (when (seq coll) >>> (let [[run etc] (split-with pred coll)] >>> (if (seq run) >>> (concat run (cons :foo (mystery-function pred etc))) >>> (cons (first coll) (mystery-function pred (rest coll)))))))) >>> 1:22 user=> (dorun (mystery-function true? (repeat true))) >>> java.lang.OutOfMemoryError: GC overhead limit exceeded (repl-1:22) >>> >>> >> Nice catch! >> >> >> -- >> Professional: http://cgrand.net/ (fr) >> On Clojure: http://clj-me.blogspot.com/ (en) >> >> >> >> > > > > > -- Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.blogspot.com/ (en) --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---