Sometimes you do want a mutable thing with thread-local binding, noir does
this for it's mutable session-flash stuff:
https://github.com/noir-clojure/lib-noir/blob/master/src/noir/session.clj#L95

I don't really recommend the approach, but I could see it being convenient.


On Mon, May 5, 2014 at 1:38 PM, John Gabriele <jmg3...@gmail.com> wrote:

> On Saturday, May 3, 2014 10:53:40 AM UTC-4, Bob Hutchison wrote:
>>
>>
>> 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.
>>
>>
> My understanding is that it's more common to *either*
>
>   * use `(def ^:dynamic *x* 1)`, *or*
>   * use `(def x (atom 1))`,
>
> but not both at the same time.
>
> That is to say, either you specifically want at dynamic var which you can
> change via a `binding`, or else you just want a global mutable (the atom).
>
> Would love to be corrected if I'm wrong though.
>
> -- John
>
>  --
> 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
> ---
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to