[Pharo-dev] Generators implemented only with blocks

Francisco Garau francisco.garau at gmail.com
Fri Oct 18 03:35:24 EDT 2013


Hi -

On 18 Oct 2013, at 06:54, Norbert Hartl <norbert at hartl.name> wrote:

> Why not just
> 
> gen := [ | current | 
>     current := 0. 
>     [ current := current + 1 ] ] value.
> 

Much concise indeed. But I am trying to have several generators instantiated from the same block definition and be able to set their initial values to any number. Just for fun, of course.

> 
> 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. 
> 
> Norbert
> 
> Am 18.10.2013 um 00:46 schrieb Francisco Garau <francisco.garau at gmail.com>:
> 
>> Hi
>> 
>> As an exercise, I've implemented a simple Generator using only blocks: 
>> 
>>     gen := 
>>         [ 
>>             [ | currentValue |
>>             Array 
>>                 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. 
>> 
>> http://live.exept.de/doc/online/english/programming/humor.html
>> 
>> 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). 
>> 
>> http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.84.7354&rep=rep1&type=pdf
>> 
>> - Francisco
>> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131018/68c638cb/attachment-0002.html>


More information about the Pharo-dev mailing list