Fwd: Generators implemented only with blocks
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&ty... - Francisco
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. Norbert Am 18.10.2013 um 00:46 schrieb Francisco Garau <francisco.garau@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&ty...
- Francisco
Hi - On 18 Oct 2013, at 06:54, Norbert Hartl <norbert@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@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&ty...
- Francisco
Am 18.10.2013 um 09:35 schrieb Francisco Garau <francisco.garau@gmail.com>:
Hi -
On 18 Oct 2013, at 06:54, Norbert Hartl <norbert@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.
genGen := [ : initial | | current | current := initial. [ current := current + 1 ] ]. gen := genGen value: 10. gen2 := genGen value: 50. (1 to: 10) inject: OrderedCollection new into: [ :col :elem | col add: gen value. col add: gen2 value. col ] ;) Norbert
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@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&ty...
- Francisco
On 18 Oct 2013, at 08:55, Norbert Hartl <norbert@hartl.name> wrote:
Am 18.10.2013 um 09:35 schrieb Francisco Garau <francisco.garau@gmail.com>:
Hi -
On 18 Oct 2013, at 06:54, Norbert Hartl <norbert@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.
genGen := [ : initial | | current | current := initial. [ current := current + 1 ] ].
gen := genGen value: 10. gen2 := genGen value: 50.
(1 to: 10) inject: OrderedCollection new into: [ :col :elem | col add: gen value. col add: gen2 value. col ]
Very nice! I also wanted to reset their values at any time. That's why I had to return blocks to initialise and provide the next value.
Norbert
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@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&ty...
- Francisco
participants (3)
-
btc@openinworld.com -
Francisco Garau -
Norbert Hartl