[Pharo-dev] Generators implemented only with blocks
norbert at hartl.name
Fri Oct 18 01:54:39 EDT 2013
Why not just
gen := [ | current |
current := 0.
[ current := current + 1 ] ] value.
I think there is two sides of using blocks. One is the ability to carry some code around which is a useful thing. The other one is that encloses over the current environement. The latter is something that you can use for quite powerful stuff. But it is also something people have problems to understand and that can easily introduce problems in code because you didn't think of what it does.
Just remember the last problem with announcements that was caused by blocks. So blocks are to me like a lot of other programming techniques: If you don't really need them don't use them.
Am 18.10.2013 um 00:46 schrieb Francisco Garau <francisco.garau at gmail.com>:
> As an exercise, I've implemented a simple Generator using only blocks:
> gen :=
> [ | currentValue |
> with: [ currentValue := 0 ]
> with: [ currentValue := currentValue + 1 ]
> ] value
> ] value.
> gen first value.
> next := [:a | a last value].
> getTen := [(1 to: 10) collect: [ :i | next value: gen ]].
> getTen value = #(1 2 3 4 5 6 7 8 9 10).
> getTen value = #(11 12 13 14 15 16 17 18 19 20).
> There have been some funny arguments in the past whether Blocks are a fundamental part of Smalltalk or not. My code above supports Vassili's view that we could get rid of Objects and implement everything in terms of Blocks.
> But more seriously, I find it quite interesting that Lars Bak choose not to implement Blocks for his Resilient Smalltalk. He choose to implement LIFO blocks; that is, blocks that cannot survive the creating activation context (see chapter 2.3 for more details).
> - Francisco
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Pharo-dev