Closure support

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Closure support

Alex Schenkman
Hello list:

I've seen some messages talking about incorporating closure support into Squeak/Pharo.

What does this mean?
A closure is a block, right? And Squeak has already blocks...

Thanks in advance!


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Closure support

Randal L. Schwartz
>>>>> "Alex" == Alex Schenkman <[hidden email]> writes:

Alex> What does this mean?
Alex> A closure is a block, right? And Squeak has already blocks...

A closure is a block but not all blocks are closures.

With classic smalltalk 80 and Squeak:

a := (1 to: 10) do: [:n | [n]].
b := a collect: [:each | each value].

b will have something like #(10 10 10 10 ...).

The problem is that the :n in each block ([n]) is shared as one variable.

In a true closure, which modern Squeak and Pharo provide, we get the proper
#(1 2 3 4 ... 10).  That's because each apperance of "n" is "closed" with
respect to the newly created block.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Closure support

Andy Burnett
In reply to this post by Alex Schenkman
Randal said...

<<With classic smalltalk 80 and Squeak:

a := (1 to: 10) do: [:n | [n]].
b := a collect: [:each | each value].
b will have something like #(10 10 10 10 ...).

The problem is that the :n in each block ([n]) is shared as one variable.
In a true closure, which modern Squeak and Pharo provide, we get the proper
#(1 2 3 4 ... 10).  That's because each apperance of "n" is "closed" with
respect to the newly created block.>>

Thanks for explaining the difference between blocks and closures. I think I new understand that.  And, your example raised a new question for me.

What does the inner 'block' in [:n | [n]] actually do?

Out of curiosity I tried changing it to [:n | [n*10]], expecting the numbers to be ten times bigger, but they were stil 1, 2, 3...  I also tried removing the whole block, and b still evaluated to the same values.

Cheers
Andy

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Closure support

Randal L. Schwartz
>>>>> "Andy" == Andy Burnett <[hidden email]> writes:

Andy> Randal said...
Andy> <<With classic smalltalk 80 and Squeak:

Andy> a := (1 to: 10) do: [:n | [n]].
Andy> b := a collect: [:each | each value].
Andy> b will have something like #(10 10 10 10 ...).

Argh.  That should have been collect: not do:

Andy> Out of curiosity I tried changing it to [:n | [n*10]], expecting the numbers
Andy> to be ten times bigger, but they were stil 1, 2, 3...  I also tried removing
Andy> the whole block, and b still evaluated to the same values.

Works better with collect. :)

a := (1 to: 10) collect: [:n | [n * 10]].
b := a collect: [:each | each value]

b is now #(10 20 30 40 50 60 70 80 90 100) in a closure world.  In
non-closure, it would have been 100, 100, 100...

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Closure support

K. K. Subramaniam
In reply to this post by Andy Burnett
On Friday 30 October 2009 09:29:16 pm Andy Burnett wrote:
> What does the inner 'block' in [:n | [n]] actually do?
A block is just a list of bytecodes - an anonymous method. When the 'value'
message is sent to a block, the list is evaluated and the result is returned.
In this case, the value of n (or n*10 in your second example).

HTH .. Subbu
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners