bindings & lazy-seq

24 views
Skip to first unread message

Carlos-K

unread,
Mar 25, 2011, 7:47:34 AM3/25/11
to Clojure
Hi,

Why when you create a lazy sequence, you need to do the bindings
inside the definition?, e.g.,

(lazy-seq
(binding [*in* in#]
<-- some code -->))

As opposed to make the bindings first and call the lazy-seq later,
e.g.,

(binding [*in* in#]
(lazy-seq
<-- some code -->))


Thanks

Meikel Brandmeyer

unread,
Mar 25, 2011, 8:07:45 AM3/25/11
to Clojure
Hi,

This might help:
http://kotka.de/blog/2009/11/Taming_the_Bound_Seq.html

Sincerely
Meikel

Mark Nutter

unread,
Mar 26, 2011, 8:29:12 AM3/26/11
to clo...@googlegroups.com
I'll take a stab at this and then someone can tell me if I'm
understanding it myself.

> (lazy-seq
> (binding [*in* in#]
> <-- some code -->))

This is inside the definition, so it executes whenever you actually
USE the definition--i.e. it binds *in* to in# at the time the lazy seq
is used, which is probably what you want.

> (binding [*in* in#]
> (lazy-seq
> <-- some code -->))

This is outside the definition, so it executes and binds *in* to in#
at the time the lazy seq is defined. In other words, the binding ends
as soon as the lazy seq is defined, so when you go to actually use the
seq, the binding has ended, and *in* has its original value.

How'd I do?

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Reply all
Reply to author
Forward
0 new messages