I'm still struggling with how to write the most readable, simple clojure code
to deal with dynamically bindings.
What is the graceful clojure equivalent of common lisp special variables for the following scenario.
If I were writing common lisp I'd just do something like (pardon if my lisp is rusty here):
(defvar *x* 1)
... do stuff with *x* ...
(setq *x* 2)
... do stuff with *x* ...
(let ((*x* 3)) (do stuff with *x*...)
;; do stuff with *x* that's currently at 2
The way I'm tempted to do this in clojure is
(def ^{:dynamic true} *x* (atom 1))
... do stuff with @*x* ...
(reset! *x* 2)
... do stuff with @*x* ...
(binding [*x* (atom 3)] (do stuff with @*x*))
Is that the simplest way to map between the two language scenarios?
Or is there something simpler, perhaps using some var-* apis or what have you?
--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You can also just use ‘def’ to redefine the global binding.
--
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
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/Wh1M345Y5u4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
On May 3, 2014, at 9:45 AM, Dave Tenny <dave....@gmail.com> wrote:I'm still struggling with how to write the most readable, simple clojure code
to deal with dynamically bindings.
What is the graceful clojure equivalent of common lisp special variables for the following scenario.
If I were writing common lisp I'd just do something like (pardon if my lisp is rusty here):
(defvar *x* 1)
... do stuff with *x* ...
(setq *x* 2)
... do stuff with *x* ...
(let ((*x* 3)) (do stuff with *x*...)
;; do stuff with *x* that's currently at 2
The way I'm tempted to do this in clojure is
(def ^{:dynamic true} *x* (atom 1))
... do stuff with @*x* ...
(reset! *x* 2)
... do stuff with @*x* ...
(binding [*x* (atom 3)] (do stuff with @*x*))
You can also just write it:(def ^:dynamic *x* (atom 1))which is a little less verbose.