Hi Rowdy,

The snake uses refs because idiomatic Clojure code should not require  
binding to a thread. The snake game could be extended to be a  
simulation that runs on multiple threads (or perhaps even multiple  
machines) without having to change the basic code. Normally, you  
wouldn't build a feature you don't need (YAGNI!) but in Clojure  
building the thread-safe version is just as easy (in some cases  
easier) than building a thread-local version.

point-size is immutable because I don't plan to rebind it. :-) If I  
did, it would have earmuffs (*point-size*). You are right -- Clojure  
does not enforce this distinction. Clojure is purer than some  
functional languages (e.g. Scala) and less pure than others (e.g.  
Haskell).

You can rebind macros, but in order to use them you have to compile  
some code again.

Cheers,
Stuart

>
> Why does the Snake example in the book use refs when all mutation is
> done from the EDT?
>
> To verify that, I put a call to "assert-edt" in front of every dosync
> in snake.clj. assert-edt is defined like this:
>
> (defn assert-edt [] (assert (javax.swing.SwingUtilities/
> isEventDispatchThread)))
>
> And it never throws. So why would I use transactions when Swing is
> already taking care of everything? The only reason I see is when you
> also want to mutate things for testing in the REPL.
>
> But even in that case, you could still do
> (javax.swing.SwingUtilities/invokeAndWait #(mutate-like-crazy))
>
> Second question - in the functional section of snake.clj, there is
> this function:
>
> (defn point-to-screen-rect [pt]
>  (map #(* point-size %)
>       [(pt 0) (pt 1) 1 1]))
>
> Is it considered functional (=pure?) because point-size is considered
> to be constant?
> According to the definitions of pure/functional that I've come across
> (return value only depends on function parameters, has no side
> effects) it would not be pure, as point-size can be changed, affecting
> the operation of that function.
>
> But then again in the face of dynamic rebinding (I could even rebind
> map to do something completely different), is there anything pure in
> clojure at all?
>
> And while I (still) got your attention ;), dynamic rebinding of macros
> is impossible because all references to macros disappear in the
> compilation phase, correct? Otherwise I could have just rebound dosync
> to always call assert-edt first instead of putting it in front of
> every "call" to dosync.
>
> >


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

Reply via email to