concept closure in category clojure

appears as: closures, closure, A closure, closure, closures
Clojure in Action, Second Edition

This is an excerpt from Manning's book Clojure in Action, Second Edition.

In this example, within the anonymous function being returned, scale doesn’t appear in any kind of binding occurrence—specifically, it’s neither a function parameter nor created in a let form. Within the anonymous function, therefore, scale is a free variable. Only lexically scoped variables can be free, and the return value of the form in which they appear depends on their value at closure creation time. Forms that enclose over free variables (such as the anonymous function shown previously) are called closures. Closures are an extremely powerful feature of languages such as Clojure—in fact, even the name Clojure is a play on the word.

How do you use a closure? Consider the following code:

8.3.1. Free variables and closures

Before we jump into what closures are, let’s look at the concept of free variables. A free variable is one that’s neither an argument nor a local variable. For instance, take a look at adder in this code:

The Joy of Clojure, Second Edition

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

From the figure, you can gather that the black pieces are lowercase characters and white pieces are uppercase. You can name any square on the chessboard using a standard rank-and-file notation—using a letter for the column and a number for the row. For example, a1 indicates the square at lower left, containing a white rook. This kind of structure likely isn’t optimal for an enterprise-ready chess application, but it’s a good start. You can ignore the actual implementation details for now and focus on the client interface to query the board for square occupations. This is a perfect opportunity to enforce encapsulation to avoid drowning the client in board-implementation details. Clojure has closures, and closures are an excellent way to group functions (Crockford 2008) with their supporting data.[14]

Figure 6.2. Each step of a lazy seq may be in one of two states. If the step is unrealized, it contains a function or closure of no arguments that can be called later to realize the step. When this happens, the thunk’s return value is cached instead, and the thunk itself is released as pictured in the first two lazy seq boxes, transitioning the step to the realized state. Note that although not shown here, a realized lazy seq may contain nothing at all, called nil, indicating the end of the seq.

All the preceding examples would work exactly the same. Here’s another function that creates and returns a closure in a similar way. Note again that the inner function maintains access to the outer parameter even after the outer function has returned:

(defn divisible [denom]
  (fn [num]
    (zero? (rem num denom))))

You don’t have to store a closure in a var; you can instead create one and call it immediately:

((divisible 3) 6)
;=> true

((divisible 3) 7)
;=> false

Instead of storing or calling a closure, a particular need is best served by passing a closure along to another function that will use it.

7.2. On closures

On his next walk with Qc Na, Anton attempted to impress his master by saying “Master, I have diligently studied the matter, and now understand that objects are truly a poor man’s closures.” Qc Na responded by hitting Anton with his stick, saying “When will you learn? Closures are a poor man’s object.” At that moment, Anton became enlightened.