From: ··············@gmail.com
Subject: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <1180027235.933219.18360@p47g2000hsd.googlegroups.com>
Hi,

I was surprised to discover on #lisp that the following is considered
relatively bad style:

(let ((x 0))
  (lambda () (incf x)))

That is, a closure that, when invoked, modifies a variable, x, from
its enclosing scope.  I personally find that quite readable, and since
before my Lisp days, I wrote similar code to that in Java (well, as
close as you can get anyway).

So there I am, mentioning it on #lisp, only to find that most channel
members would prefer to make x a slot in an object, and (I assume) the
lambda to be a generic function on that.  I couldn't get a reason -
readability was mentioned, but I find the (let) perfectly readable.

I was also accused of using (let) to emulate OOP, which surprised me,
as I often consider some uses of OOP in Java an emulation of (let).  I
was also accused of premature optimisation, which I'm certainly not
doing.

My thoughts are that x is a local variable that has no relevance
outside the scope it's used in, so I don't know why one would want to
put it into an object and hand out that object.  It's not that I don't
trust myself not to access x directly, but that I don't see a reason
to expose it at all.

Is my thinking broken?  If so, what would you suggest I do to fix it?

Cheers,
Ricky.

From: ··············@gmail.com
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <1180027803.276669.126090@k79g2000hse.googlegroups.com>
It appears that I've misrepresented the channel here, so you may
disregard the above in terms of how it paints #lisp, but I'm still
interested to see why and when various people would use CLOS over a
closure.
From: Joe Marshall
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <1180030810.650415.114990@q19g2000prn.googlegroups.com>
On May 24, 10:30 am, ···············@gmail.com"
<··············@gmail.com> wrote:
> It appears that I've misrepresented the channel here, so you may
> disregard the above in terms of how it paints #lisp, but I'm still
> interested to see why and when various people would use CLOS over a
> closure.

I'd make it an object if it makes sense for it to be an object.  That
is, if the concept is general enough that it obviously stands on its
own.

How's that for vague?
From: Dan Bensen
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <f34muv$13j$1@wildfire.prairienet.org>
··············@gmail.com wrote:
> why and when various people would use CLOS over a closure.
When there are at least two methods accessing the data.

-- 
Dan
www.prairienet.org/~dsb/
From: Pillsy
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <1180044068.521153.63020@p77g2000hsh.googlegroups.com>
On May 24, 1:30 pm, ···············@gmail.com"
<··············@gmail.com> wrote:
> It appears that I've misrepresented the channel here, so you may
> disregard the above in terms of how it paints #lisp, but I'm still
> interested to see why and when various people would use CLOS over a
> closure.

If it were something that simple, I'd probably just make it a closure.
I haven't really thought about why too much, beyond the fact that it
feels like the most natural abstraction to use. You call a function,
you get the number of times it's been called, and it's all very
simple.

Generally I'll define a class if I think that (a) I might need
polymorphism or (b) I might need inheritance. With a little counter
like that, I wouldn't expect to need either.

Cheers, Pillsy
From: Rob St. Amant
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <f359tu$fq$1@blackhelicopter.databasix.com>
Pillsy <·········@gmail.com> writes:

> On May 24, 1:30 pm, ···············@gmail.com"
> <··············@gmail.com> wrote:
>> It appears that I've misrepresented the channel here, so you may
>> disregard the above in terms of how it paints #lisp, but I'm still
>> interested to see why and when various people would use CLOS over a
>> closure.
>
> If it were something that simple, I'd probably just make it a closure.
> I haven't really thought about why too much, beyond the fact that it
> feels like the most natural abstraction to use. You call a function,
> you get the number of times it's been called, and it's all very
> simple.

Actually, if it were that simple, I expect most Lispers would write

(let ((x 0))
  (incf x (<function call>)))

rather than passing a closure as an argument to modify x.  I assume
the real scenario is more complicated, of course.
From: Ken Tilton
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <Sfm5i.267$lB1.259@newsfe12.lga>
··············@gmail.com wrote:
> It appears that I've misrepresented the channel here, so you may
> disregard the above in terms of how it paints #lisp, but I'm still
> interested to see why and when various people would use CLOS over a
> closure.
> 

This is like listening to one side of a lover's spat when some idiot 
decides to conduct one in public over a cell phone, ie, pretty cool.

Closed over vars have their place. Tried it with Cells. Wasn't any 
faster, was much hairier than structs.

kt
From: Geoff Wozniak
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <1180034334.769104.225350@g4g2000hsf.googlegroups.com>
··············@gmail.com wrote:
> (let ((x 0))
>   (lambda () (incf x)))
>

I only use these kinds of lexical closures when I want the data to be
private and I'm quite sure it will never be public.  I tend to prefer
using slots in an object since it's more amenable to future change.

Note that when you do this, making an interface to manipulate the
private elements can be a pain.
From: John Thingstad
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <op.tsujew0tpqzri1@pandora.upc.no>
On Thu, 24 May 2007 19:20:35 +0200, ··············@gmail.com  
<··············@gmail.com> wrote:

> Hi,
>
> I was surprised to discover on #lisp that the following is considered
> relatively bad style:
>
> (let ((x 0))
>   (lambda () (incf x)))
>

Closures can be considered modular programming.
Werther you prefer modular to object oriented is much a matter of taste as  
well that what you prefer to do.

The case for objects is that it would be easier to extend to make multiple  
objects.
This might increase the reuseability.
Closures are however faster..


-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Rainer Joswig
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <joswig-AACF03.22230724052007@news-europe.giganews.com>
In article <·······················@p47g2000hsd.googlegroups.com>,
 ···············@gmail.com" <··············@gmail.com> wrote:

> Hi,
> 
> I was surprised to discover on #lisp that the following is considered
> relatively bad style:
> 
> (let ((x 0))
>   (lambda () (incf x)))
> 
> That is, a closure that, when invoked, modifies a variable, x, from
> its enclosing scope.  I personally find that quite readable, and since
> before my Lisp days, I wrote similar code to that in Java (well, as
> close as you can get anyway).
> 
> So there I am, mentioning it on #lisp, only to find that most channel
> members would prefer to make x a slot in an object, and (I assume) the
> lambda to be a generic function on that.  I couldn't get a reason -
> readability was mentioned, but I find the (let) perfectly readable.
> 
> I was also accused of using (let) to emulate OOP, which surprised me,
> as I often consider some uses of OOP in Java an emulation of (let).  I
> was also accused of premature optimisation, which I'm certainly not
> doing.
> 
> My thoughts are that x is a local variable that has no relevance
> outside the scope it's used in, so I don't know why one would want to
> put it into an object and hand out that object.  It's not that I don't
> trust myself not to access x directly, but that I don't see a reason
> to expose it at all.
> 
> Is my thinking broken?  If so, what would you suggest I do to fix it?


This style is useful in many places. Use it when you
want to create a function which should be used
later in some place, but still referring to some
values.

It is not useful if you try to model objects with it.
For programming with objects, CLOS is the preferred
approach.

The drawbacks with closures:

* there is no standard way to look inside. That's okay,
  but means that you use non-standard tools for debugging.

* objects can be of different classes. closures are just closures.

and some more...

> 
> Cheers,
> Ricky.

-- 
http://lispm.dyndns.org
From: John Thingstad
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <op.tsump0s3pqzri1@pandora.upc.no>
On Thu, 24 May 2007 19:20:35 +0200, ··············@gmail.com  
<··············@gmail.com> wrote:

>
> Is my thinking broken?  If so, what would you suggest I do to fix it?
>

You might want to look at the code for cl-ppcre (Edi Weitz) and chapter 3  
of on Lisp (Paul Gaham)
for a example of where closures are a very effective tool.



-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Timofei Shatrov
Subject: Re: Prefer CLOS over closures that modify variables they close over?
Date: 
Message-ID: <46567736.1756525@news.readfreenews.net>
On 24 May 2007 10:20:35 -0700, ···············@gmail.com"
<··············@gmail.com> tried to confuse everyone with this message:

>Hi,
>
>I was surprised to discover on #lisp that the following is considered
>relatively bad style:
>
>(let ((x 0))
>  (lambda () (incf x)))
>
>That is, a closure that, when invoked, modifies a variable, x, from
>its enclosing scope.  I personally find that quite readable, and since
>before my Lisp days, I wrote similar code to that in Java (well, as
>close as you can get anyway).

If you want to save the state of your program with cl-store or something
similar, you'll run into problems with your closure approach. There are some
domains where such closures are useful, for example memoization of function's
return value, but for handling of data, classes and structs are better in many
ways.

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|