On Monday 05 January 2009 09:35, wubbie wrote:
> Hi,
> This example is from clojure site.
> My question is on line 5 and line 6:
> The confusion is str is a function and here looks like used as a
> regular variable.
>
> Thanks in advance.
> Sun
>
>
> (defn loves [x y]
>   (str x " loves " y))
> (defn test-rebind []
>   (println (loves "ricky" "lucy"))
>   (let [str-orig str]    --->  line 5:
>     (binding [str (fn [& args]    --> line 6:
>                     (println "Logging str")
>                     (apply str-orig args))]    -->line 8:
>       (println (loves "fred" "ethel")))))
> (test-rebind)

The value held by the global Var named "str" is saved in str-orig. Then 
inside the let that binds str-orig, a local override of that global 
binding is established that essentially, within the scope of that 
(binding ...) form, redefines str. This is a dynamically scoped binding 
in the classic sense that when code that invokes (str ...) is called 
somewhere "beneath" or "inside" this local (binding ...), it will get 
the logging form of (str), which, after printing its logging output, 
defers to the original str function via the value saved in str-orig. 
Other calls to (str ...) (in other threads), even if they occur 
concurrent to the execution of loves will not be affected by this local 
binding.

Neat, eh?


> ricky loves lucy
> Logging str
> Logging str
> Logging str
> Logging str
> Logging str
> fred loves ethel


Randall Schulz

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