Re: Closures




H. wrote:
> So I'm trying to get my head around exactly what a "closure" is. I've
> read what SICP says about closures via all of the references listed in
> the index. I understand the general concept of closures, but not the
> full implications. Part of my problem is that I have a lot of other
> terms running through my head from other languages (namely, JavaScript,
> Java, C++), and they seem to be getting in the way of understanding,
> instead of facilitating it!

The way I think of closures is that they are a chunk of code with
perhaps *some* but not necessarily *all* the variables bound.

> At first, I kind of thought of a closure as a block of code with its
> own scope.

In general, you want the bound variables in the closure to be
more-or-less private (nested closures have nested scope, so its not
*exacty* private). The Lisp machine allowed you to close over the
binding cells of special-bound variables, but that was a *real* hack.

> Then I read the wikipedia definition of closure, and it said
> that closures store information across function calls, so I thought of
> static variables in these blocks of code.

They are quasi-static. If I make a closure, nothing says I can only
use it once. The closed-over bindings will persist and be present on
each call to the closure.

> Recently I read a closure was a "generic representation of a callback"
> (http://www.le-hacker.org/papers/gobject/ch03.html)

Heh, heh. Not `a callback is a primitive, half-working implementation
of a closure'?
Again, we have code we will invoke, *some* of the bindings are
established (when we create the callback) and the remaining are
established when the callback is invoked.
The difference with a callback is that it is expected that the closure
will have a dynamic extent not much larger than the procedure called.
(So you can allocate the closure on the stack and destroy it when the
called procedure returns rather than leaving it up to the GC.

> When I think "callback", the example that comes to mind is one in
> JavaScript. There is a built in sort method which has default
> alphabetical behavior. However it can be modified to work in a
> different way (e.g. numerically), or even on different types, like:
>
> // Assume an array of Person objects might need to be sorted by age or
> name
> // That's easy. Just create two callbacks.
> function ageCallback(a,b)
> {
> return a.age - b.age;
> }
>
> function nameCallback(a,b)
> {
> if (name.a < name.b)
> return -1;
> else if (name.b > name.a)
> return 1;
> else return 0;
> }
>
> arrayOfPersonObjects.sort(ageCallback); // sort by age
> arrayOfPersonObjects.sort(nameCallback); // sort by name
>
> This is a nifty ability, I think. But, um, I can't figure out what it
> has to do with closures! And I'm getting kind of confused trying to
> put all of these "closure" definitions together. Yeah, so any help
> would be great.

In this example, you aren't really closing over anything in the
callback. Let me make it a bit more complicated. Let's suppose you
have defined your own special array-like data structure that you'd like
to sort. But your data structure allows the user to specify the sort
key at the time the structure is created. I'll write this in Scheme
because I don't want to spend the afternoon debugging JavaScript.

(define make-my-structure cons)
(define sort-key car)
(define underlying-sequence cdr)

(define (make-sort-callback sort-key)
(lambda (l r)
(< (sort-key l) (sort-key r))))

(define (sort-my-sequence ms)
(sort (underlying-sequence ms) (make-sort-callback (sort-key ms))))

The callback in sort needs to know the sort key, but we can't write a
specific callback for each sort key because we don't know what the user
is going to choose. Instead, we contruct the sort callback by closing
over the sort key and hand this callback to the sort procedure.

.



Relevant Pages

  • Re: Securing a future for anonymous functions in Python
    ... >>to handle simple manipulation of callback arguments (e.g., ... Not sure what you mean by closure here. ... external environment needed to be captured for use by a function definition ... for a lambda expression, ISTM. ...
    (comp.lang.python)
  • Closures
    ... So I'm trying to get my head around exactly what a "closure" is. ... Recently I read a closure was a "generic representation of a callback" ... There is a built in sort method which has default ...
    (comp.lang.scheme)
  • Re: How come Ada isnt more popular?
    ... Maciej Sobczak writes: ... A lot of OO models with callback also require cycles. ...
    (comp.lang.ada)
  • Re: Dan Simmons on Larry Niven?
    ... If you want a sort of re-tread of Chaucer, ... "Hyperion" and found it less than wonderful. ... As an INTJ, I do like closure. ...
    (rec.arts.sf.written)
  • Re: Bits from a stream
    ... to reset the reader's closure. ... then does a (read-bit-reset), which I could then encapsulate out by ... Is there some sort of license to the code you posted a pointer to? ...
    (comp.lang.lisp)