Hi,
On 26 Nov., 23:40, André Thieme <[EMAIL PROTECTED]> wrote:
> Maybe a simple accumulator could work here:
> user> (let [acc (ref 0)]
> (defn make-accumulator [n]
> (dosync (ref-set acc n))
> (fn [i]
> (dosync (alter acc + i)))))
> #'user/make-accumulator
>
> So, here make-accumulator is a closure.
> It closes over acc.
> And the anon function that is returned by
> make-accumulator is also a closure, as it
> also closes over acc.
Maybe I have a wrong understanding of "closure", but
as far as I understand, every function definition in
Clojure - be it defn or #() - finally translates to (fn ...).
And fn creates a closure. In fact #(prn "Hello") closes
over the var "prn". Consider the following example:
(ns foo.bar)
(def my-closure #(prn "Hello"))
(ns yoyo.dyne
(:refer-clojure
:exclude (prn)))
(defn prn [x] (clojure.core/prn (str "Modified: " x)))
(foo.bar/my-closure)
According to my understanding the output should
be "Hello".
I think the closure is never "empty". It refers to the
environment used at the closure's creation time.
Whether this environment is accessed or not, is not
very interesting, I think.
Ok. Maybe there are gc issues, so one might want
to optimise this. But a closure which, does not
access the environment at all... What would this
look like and which use would it have?
Sincerely
Meikel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---