On Thu, Jul 9, 2009 at 7:56 PM, Conrad<drc...@gmail.com> wrote: > > I'm trying to optimize an inner loop and need a variable that mutates > to make this work. It does NOT need to be a thread-safe variable. > What's the best way to create a "plain ol' mutating variable" in > Clojure? I know I can always use an Atom, but I was wondering if > there's a more low-level way (besides using the java interop, since I > may still want this variable to be dynamically typed.)
You can try with-local-vars. I'm not sure of the performance characteristics of this versus using an atom, but it certainly feels more imperative: user=> (doc with-local-vars) ------------------------- clojure.core/with-local-vars ([name-vals-vec & body]) Macro varbinding=> symbol init-expr Executes the exprs in a context in which the symbols are bound to vars with per-thread bindings to the init-exprs. The symbols refer to the var objects themselves, and must be accessed with var-get and var-set nil user=> (with-local-vars [i 0 j 0] (doseq [k (range 10)] (println "i * j =" (* (var-get i) (var-get j))) (var-set i (inc (var-get i))) (var-set j (dec (var-get j))))) i * j = 0 i * j = -1 i * j = -4 i * j = -9 i * j = -16 i * j = -25 i * j = -36 i * j = -49 i * j = -64 i * j = -81 nil HTH, - J. --~--~---------~--~----~------------~-------~--~----~ 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 Note that posts from new members are moderated - please be patient with your first post. 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 -~----------~----~----~----~------~----~------~--~---