Ah, and also it's not lazy, e.g. :

(take 10 (reduce #(concat %1 (if (> %2 6) [ :foo %2] [%2])) []
(iterate inc 0))))

Since  you invited comments, please note that the quick way of writing
a literal list '(3 4 5 6 8) is not general, because it does not
evaluate the elements of the list :
Try (let [a 0] '(a 1 2 3)) for example and  you may (or may not, I
remember I was, since it is a so much spread idiom) be surprised.

The version that will always give what you expect is just with
function list : (let [a 0] (list a 1 2 3))

HTH,

-- 
Laurent

will never return, for example.

2009/4/23 Laurent PETIT <laurent.pe...@gmail.com>:
> 1:2 user=> (reduce #(concat %1 (if (> %2 6) [ :foo %2] [%2])) [] '(3 4 5 8 4 
> 2))
> (3 4 5 :foo 8 4 2)
>
> Not good, the expected result would have been
> (3 4 5 :foo 8 4 2 :foo)
>
> Regards,
>
> --
> Laurent
>
> 2009/4/23 Emeka <emekami...@gmail.com>:
>>
>> (reduce #(concat %1 (if (> %2 6) [ :foo %2] [%2])) [] '(3 4 5 8 4 2))
>>
>> Note that I am not a programmer and do not know much about writing code,
>> however the above snippet in my view can achieve the result you desired. I
>> invite comments on the above.
>>
>> Regards,
>> Emeka
>> On Thu, Apr 23, 2009 at 10:52 AM, Christophe Grand <christo...@cgrand.net>
>> wrote:
>>>
>>> *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
-~----------~----~----~----~------~----~------~--~---

Reply via email to