Re: "Staggering power of closures..."

From: Dave Roberts (ldave-re-move_at_re-move.droberts.com)
Date: 03/29/04


Date: Mon, 29 Mar 2004 01:44:46 GMT

Peter Seibel wrote:

> The difference is mostly one of convenience. When I write:
>
> (defun foo (x y z things)
> (mapcar #'(lambda (thing) (frob x y thing)) things))
>
> I don't have to think about making a class to hold the X and Y values.
> And when I decide I want to change that to:
>
> (defun foo (x y z things)
> (mapcar #'(lambda (thing) (super-frob x y z thing)) things))
>
> I don't have to also go edit my class definition to add a field to
> hold the Z value. Inner classes, give this part of the convenience of
> closures since the compiler figures out which local variables I'm
> refering to and creates a class with just the right fields to hold
> them and under the covers calls a constructor with the local variables
> as arguments so their values can be used to initialize the fields.

Right, good point. So this is pretty nice. "Low overhead" is I guess the way
I would put it.

> The difference of course, as various folks have pointed out, is that
> the instance of the inner class has a copy of the *value* not a
> reference to the actual variable, so it's trickier to write the
> equivalent of this:
>
> (defun foo (things)
> (let ((count 0))
> (values (mapcar #'(lambda (thing)
> (when (good-p thing) (incf count))
> (frob thing)) things)
> count)))
>
> (The nearest translation in Java would involve making count a mutable
> object such as a one-element array. Which is pretty gross and liable
> to confuse the heck out of your average Java programmer.)

Yup. The syntax is just so much cleaner in general. I fully agree with that.
As you say, you could do it in Java, but the amount of pain goes up
substantially, which sort of steers you away from using. With Lisp, it's a
lot easier and so you're more likely to use it.

-- 
Dave Roberts
ldave-re-move@re-move.droberts.com


Relevant Pages

  • Re: Newbie (Post#1): Help with Array of Objects
    ... In java, unlike member ... > variables, local variables are not implicitly initialized, and you cannot ... sense if you picture yourself in a war against the compiler to make your ... The code always fails at runtime. ...
    (comp.lang.java.help)
  • Using final with local variables
    ... A question for the Java gurus. ... That makes sense because the compiler is directly writing machine code ... and can make use of the hint if it wants to. ... I am talking about local variables, ...
    (comp.lang.java.programmer)
  • Re: Garbage collection problem
    ... > variable arise because Java does not actually have nested local variable ... local variables are 'slots' in the stack ... removing the reachability of references in the reused slots. ... FirstSQL/J Object/Relational DBMS ...
    (comp.lang.java.programmer)
  • Re: SQL and the LGP-30
    ... What needs to happen is a tighter integration of Java and SQL. ... Then you could do compile time checking to make sure column names are ... correct and the types of local variables could be generated from the ... SQL allows fully dynamic work that could not be type checked, ...
    (comp.lang.java.databases)
  • Re: a good book to start learning Java
    ... Peter Seibel wrote: ... > Assuming you're a somewhat experienced programmer, grab _The Java ... There are lots of tricks we can do using the namesapces of Lisp. ... already available and I really understood what a macro really does! ...
    (comp.lang.lisp)