Hi,

2009/5/8 Mark Reid <mark.r...@gmail.com>

>
> Hi,
>
> This `lazy-seq` over a `when` and `cons` idiom seems fairly common. Is
> there any reason there is not a function for it? For example:
>
> (defn cons-while
>  "Lazily creates a sequence by repeatedly calling f until pred is
> false"


s/false/logical false/

>
>  [pred f]
>  (lazy-seq
>    (when pred
>      (cons f (cons-while pred f)))))
>
> I haven't tested it but I suspect the following should now work:
>
> (cons-while (Mouse/hasEvent) (Mouse/getEvent))


(cons-while #(Mouse/hasEvent) #(Mouse/getEvent))

or maybe create a general version of repeatedly-while :

(defn repeatedly-while [pred f] (take-while pred (repeatedly f)))

and call it like this also:

(repeatedly-while #(Mouse/hasEvent) #(Mouse/getEvent))

Then I remark it is just a minor variation on Meikel's first answer :

(take-while #(no-op-pred) (repeatedly f)))


(not tested)


>
> Regards,
>
> Mark
> --
> http://mark.reid.name
>
> On May 8, 7:44 am, Meikel Brandmeyer <m...@kotka.de> wrote:
> > Hi,
> >
> > lazy-seq to the rescue:
> >
> > (defn mouse-seq
> >    []
> >    (lazy-seq
> >      (when (Mouse/hasEvent)
> >        (cons (Mouse/getEvent) (mouse-seq)))))
> >
> > Sincerely
> > Meikel
> >
> > Am 07.05.2009 um 23:40 schrieb CuppoJava:
> >
> >
> >
> >
> >
> > > Yeah (pred) is not supposed to depend on any items inside f.
> >
> > > This is why (take-while pred (repeatedly f)))
> > > won't work in this situation.
> >
> > > (take-while) will always take an element out of f, so that it can be
> > > tested using (pred). I don't want any elements of (f) to be looked at
> > > if (pred) is false.
> >
> > > ----USE CASE----
> > > I'm using it in combination with some Java libraries. The following
> > > seems like a very clojure-ish way of doing things.
> >
> > > (for [:while (Mouse/hasEvent)] (Mouse/getEvent))
> >
> > > so this returns a nice lazy stream of mouse events, which can be
> > > processed however i like.
> > > >
> >
> >
> >  smime.p7s
> > 5KViewDownload
>
> >
>

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