Laurent,

Sampi question was;

Let's say I have a sequence of integers:
 (def a (3 9 1 5 102 -322 ...))

Is there a function for inserting an object—let's say :foo—after
elements that fulfill a certain predicate?
Furthermore, I mean inserting :foo after any block of elements that
fulfill it:

 (mystery-function (partial > 6) a) ; -> (3 :foo 9 1 5 :foo 102
-322 :foo ...)

I didn't know that :foo should also be the last element of the list. Did he
actually asked for this?
Thanks for pointing out the subtle difference between the two versions of
list.

Regards,
Emeka


On Thu, Apr 23, 2009 at 12:37 PM, Laurent PETIT <laurent.pe...@gmail.com>wrote:

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