Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a closure? If so, why?

While preparing an answer to another question, I created one for myself. Consider the following short program.

(ns net.dneclark.JFrameAndTimerDemo
  (:import (javax.swing JLabel JButton JPanel JFrame Timer))
  (:gen-class))

(defn timer-action [label counter]
 (proxy [java.awt.event.ActionListener] []
   (actionPerformed
     [e]
      (.setText label (str "Counter: " (swap! counter inc))))))

(defn timer-fn []
  (let [counter (atom 0)
        label (JLabel. "Counter: 0")
        timer (Timer. 1000 (timer-action label counter))
        panel (doto (JPanel.)
                (.add label))]
    (.start timer)
    (doto (JFrame. "Timer App")
      (.setContentPane panel)
      (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
      (.setLocation 300 300)
      (.setSize 200 200)
      (.setVisible true))))

(defn -main []
  (timer-fn))

In the action listener, 'timer-action', the value of the 'counter' argument is altered. The actual variable is declared in the 'timer-fn' function, but is altered in the listener. In my earlier experience with languages like Pascal, I would have considered 'counter' to be passed by reference. Is that the case here or is this an example of a closure? Something else?

Thanks for the help.

like image 910
clartaq Avatar asked Jan 26 '11 14:01

clartaq


People also ask

How do you ask for a closure?

Be As Formal As Possible. The best way to get closure is by having a controlled conversation, instead of one that gets heated. You can do so by scheduling a time to talk on the phone, or even meeting formally for coffee, if that feels right. Once you meet up, set the tone by being the first one to speak.

What closure really means?

Closure means finality; a letting go of what once was. Finding closure implies a complete acceptance of what has happened and an honoring of the transition away from what's finished to something new. In other words, closure describes the ability to go beyond imposed limitations in order to find different possibilities.

Why closure is so important?

Closure is important after a breakup because:Your brain needs an authentic narrative to make sense of what happened. Without closure you might keep going back to a relationship that wasn't working. You could be doomed to repeat the same relationship patterns the next time around without closure.

How do you know you need closure?

Psychologists think of closure as the desire for an answer that leaves no room for uncertainty. When we say a person has a need for closure, we're saying they're seeking the answers and resolution that they need to move on. People seeking closure are motivated by the benefits it can provide.


1 Answers

Yes, it's a closure. The lexical context of the handler function definition is preserved, and when it is later invoked it can access and update variables that "live" there.

I'm not sure how to answer the question, "why?" other than to point out that it's simply the way the language is defined to work.

like image 141
Pointy Avatar answered Oct 05 '22 23:10

Pointy