I am trying to generate a var for use in a generator for a stream. As  
a simple example, let's try to create a stream of integers. My first  
attempt at the generator was:

(with-local-vars [n 0]
   (defn integers [_]
     (let [current (var-get n)]
       (var-set n (inc current))
       current)))

I thought this would create a var n initialized to zero that would be  
captured in the closure. But it doesn't work:

(integers nil)
java.lang.IllegalStateException: Var null/null is unbound.  
(NO_SOURCE_FILE:0)


Next try: create the local inside the function:

(defn integers [_]
   (with-local-vars [n 0]
     (let [current (var-get n)]
       (var-set n (inc current))
       current)))

This works but, as I expected, the var is initialized at every call,  
so the generator returns zero forever.

I didn't find any other way to create a local var, so I am a bit at a  
loss. Any ideas?

Konrad.


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