[Newbies] Closure support

Randal L. Schwartz merlyn at stonehenge.com
Fri Oct 30 16:10:46 UTC 2009


>>>>> "Andy" == Andy Burnett <andy.burnett at knowinnovation.com> 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
<merlyn at stonehenge.com> <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


More information about the Beginners mailing list