Hi,
I wanted to try some experiments with BlockClosures on 3.9, but I can't find how to make the Compiler to use them.
For example, If I inspect [Transcript show: 'hello world'] I get a BlockContext instance, not a BlockClosure one. Is that correct ? Do I need to do something to start using BlockClosure ?
Thanks in advance
Regards, Hernán
___________________________________________________________ 1GB gratis, Antivirus y Antispam Correo Yahoo!, el mejor correo web del mundo http://correo.yahoo.com.ar
Hernán,
Block closures are truly wonderful things in Smalltalk. It does take a second to wrap your mind around them, but once that is done they are almost as addictive as associations.
The basic idea is to wrap up a section of code to be used later and it allows you to pass in variables use during execution.
In your example you are saying execute this code with no variables. So "[aFoo bar] value" would give you the same thing as "aFoo bar". You could make your block work by doing "[Transcript show: 'hello world'] value" but there is little value in doing that.
You might instead try: [:a | Transcript show: a] value: 'Hello World'.
Which reads execute the block [:a | Transcript show: a] with the value of 'Hello World' substituted for the temp variable :a.
Or even try: [:a :b | Transcript show: a; cr; show: b] value: 'Hello' value: 'World'
Which reads as execute the block [:a :b | Transcript show: a; cr; show: b] with the values of 'Hello' and 'World' substituted for the temp variable :a and :b. (the ; cr; is a cascade that says send cr to Transcript to cause a carriage return).
But wait there's more. Since Block is an instance of a class you can pass it around!
MyClass class >> salutationBlock
^[:a | Transcript show: ('Hello World especially ', a)]
And now you can execute: MyClass salutationBlock value: 'Ron'.
Or
MyClass >> myFriend ^'Ron'.
MyClass >> sayHelloToMyLittleFriend: aBlock aBlock value: self myFriend.
aMyClass sayHelloToMyLittleFriend: aMyClass class salutationBlock.
There is more since blocks to take the context with them when they are created but I'm thinking that's enough for now.
It really is a lot of fun, and extremely powerful once you understand how it works. Just be careful. They are addictive and I've seen block programming that became more of a curiosity then a necessity. (I should talk you should see what I did with associations!)
Ron Teitelbaum
-----Original Message----- From: squeak-dev-bounces@lists.squeakfoundation.org [mailto:squeak-dev- bounces@lists.squeakfoundation.org] On Behalf Of Hernan Tylim Sent: Wednesday, January 04, 2006 9:47 AM To: The general-purpose Squeak developers list Subject: Block Closures
Hi,
I wanted to try some experiments with BlockClosures on 3.9, but I can't find how to make the Compiler to use them.
For example, If I inspect [Transcript show: 'hello world'] I get a BlockContext instance, not a BlockClosure one. Is that correct ? Do I need to do something to start using BlockClosure ?
Thanks in advance
Regards, Hernán
1GB gratis, Antivirus y Antispam Correo Yahoo!, el mejor correo web del mundo http://correo.yahoo.com.ar
It really is a lot of fun, and extremely powerful once you understand how it works. Just be careful. They are addictive and I've seen block programming that became more of a curiosity then a necessity. (I should talk you should see what I did with associations!)
Ok, I'll bite. What did you do with associations?
Hi.
I put a refactored version of Scamper into SqueakMap.
This has additional functions such as a.. multilingual web page support b.. auto selection of Japanese encoding c.. Smalltalk script support http://map1.squeakfoundation.org/sm/account/package/522c5a51-a908-4f69-b3a8-...
Because several detailed changes are also done outside the package, please take a look those changes before installing.
Enjoy! - ICHIKAWA, Yuji
ICHIKAWA, Yuji wrote:
Hi.
I put a refactored version of Scamper into SqueakMap.
This has additional functions such as a.. multilingual web page support b.. auto selection of Japanese encoding c.. Smalltalk script support http://map1.squeakfoundation.org/sm/account/package/522c5a51-a908-4f69-b3a8-...
Because several detailed changes are also done outside the package, please take a look those changes before installing.
Enjoy!
ICHIKAWA, Yuji
Cool, I have to take a look. The link seems to be http://map1.squeakfoundation.org/sm/package/522c5a51-a908-4f69-b3a8-d7dcb1ae...
Karl
Joerg,
Well keep in mind that I just learned Smalltalk when I stumbled across associations. They were without a doubt the greatest thing I'd ever seen.
First and association is a key value pair. In a workspace type in 'a'->'b' highlight the whole thing and select inspect it. You get back an association whose key is 'a' and a value of 'b'. The keys and values can be any sort of object including other associations or collections. This is pretty handy.
For what ever reason Associations just clicked for me and I saw tremendous power in them even more power then a dictionary which I only realized later was a collection of associations with some heavy weight clothing that hid my powerful associations.
One of the first things I wrote was
selectUnique: aBlock "Returns to the sender a collection of assoications with the result of aBlock as the key and a collection of matching items as a value" "Testing: #(#a #a #b) selectUnique: [:a | a]"
| result | result := self species new. self do: [:anObject | |myValue myAssoc newAssoc | myValue := (aBlock value: anObject). myAssoc := result detect: [:a | a key = myValue] ifNone: [result := result copyWith: (newAssoc := myValue->OrderedCollection new). newAssoc]. myAssoc value add: anObject. ]. ^ result
This was truly amazing and allowed me to write really interesting code. Like
((aPeopleCollection selectUnqiue: [:a | a city]) collect: [:cityPeople | cityPeople value collect: [:aPerson | aPerson -> cityPeople key -> cityPeople key taxRate * aPerson baseSalary]]) merge
or something silly like that. Which returns a collection of associations that contain a person their city and their base salary * the cities taxRate. Merge is another story altogether but it is something I wrote that merges collections of collections together.
Before you knew it I was slicing and dicing all kinds of values and creating nested associations you wouldn't believe, but that were terrific for doing all sorts of things.
Next I was writing code for storing information in associations in databases hanging associations off of objects and using associations for linking. We even had to write a transform for toplink so that I could store all my silly associations in oracle.
I had a circular linked list problem once that I solved with associations. anA -> aB -> aC -> anA worked terrific to allow paging trough a circular list.
I've grown up since then (some) and have learned more appropriate ways to do things like real linked lists but powerful tools like selectUnique die hard.
By the way squeak has a feature groupBy: aGroupingBlock having: aFilterBlock which will work like selectUnqiue if you use a true block for the having: for example (aPeopleCollection groupBy: [:a | a city] having: [:a | true]). See http://bugs.impara.de/view.php?id=1821 but it does return a dictionary (fully clad collection of Associations armor and all).
I think it was a good learning experience but don't follow my example. Much of what I did I could have done with real objects and saved myself a lot of time counting key key key value, or value key value value.
Happy programming,
Ron Teitelbaum Ron@USMedRec.com
-----Original Message----- From: squeak-dev-bounces@lists.squeakfoundation.org [mailto:squeak-dev- bounces@lists.squeakfoundation.org] On Behalf Of Joerg Beekmann Sent: Tuesday, January 10, 2006 11:06 AM To: Ron@USMedRec.com; The general-purpose Squeak developers list Subject: Re: Block Closures
It really is a lot of fun, and extremely powerful once you understand how
it
works. Just be careful. They are addictive and I've seen block
programming
that became more of a curiosity then a necessity. (I should talk you
should
see what I did with associations!)
Ok, I'll bite. What did you do with associations?
-- Joerg Beekmann DeepCove Labs 4th floor 595 Howe Street Vancouver, BC, V6C 2T5 joerg@deepcovelabs.com
Ron Teitelbaum wrote:
I think it was a good learning experience but don't follow my example. Much of what I did I could have done with real objects and saved myself a lot of time counting key key key value, or value key value value.
Ahh that takes me back to my good old university days... (car (car (cdr (car (cdr (cdr .... :)
Julian
Ron Teitelbaum wrote:
I think it was a good learning experience but don't follow my example. Much of what I did I could have done with real objects and saved myself a lot of time counting key key key value, or value key value value.
caaadr, cdaddr ... ;)
On 04.01.2006, at 15:47, Hernan Tylim wrote:
Hi,
I wanted to try some experiments with BlockClosures on 3.9, but I can't find how to make the Compiler to use them.
For example, If I inspect [Transcript show: 'hello world'] I get a BlockContext instance, not a BlockClosure one. Is that correct ?
Yes
Do I need to do something to start using BlockClosure ?
You need to install the package "NewCompiler", the latest is on SqueakSource. Then open the preference browser and enable "compileUseNewCompiler" and "compileBlocksAsClosures".
Please note that I have not yet used the NewCompiler in the latest 3.9 (with Traits), so it might not work right now...
Now with the Traits integration done, I will integrate into 3.9 all overwrites / extensions needed for runtime that are right now in the NewCompiler package. This will be done next week.
Marcus
Hehehe! Should have read closer. Sorry about that!
Ron Teitelbaum
-----Original Message----- From: squeak-dev-bounces@lists.squeakfoundation.org [mailto:squeak-dev- bounces@lists.squeakfoundation.org] On Behalf Of Marcus Denker Sent: Wednesday, January 04, 2006 10:40 AM To: The general-purpose Squeak developers list Subject: Re: Block Closures
On 04.01.2006, at 15:47, Hernan Tylim wrote:
Hi,
I wanted to try some experiments with BlockClosures on 3.9, but I can't find how to make the Compiler to use them.
For example, If I inspect [Transcript show: 'hello world'] I get a BlockContext instance, not a BlockClosure one. Is that correct ?
Yes
Do I need to do something to start using BlockClosure ?
You need to install the package "NewCompiler", the latest is on SqueakSource. Then open the preference browser and enable "compileUseNewCompiler" and "compileBlocksAsClosures".
Please note that I have not yet used the NewCompiler in the latest 3.9 (with Traits), so it might not work right now...
Now with the Traits integration done, I will integrate into 3.9 all overwrites / extensions needed for runtime that are right now in the NewCompiler package. This will be done next week.
Marcus
On Jan 4, 2006, at 11:06, Ron Teitelbaum wrote:
Hehehe! Should have read closer. Sorry about that!
Ron Teitelbaum
While you didn't answer the original question I found your explanation and examples of 'blocks' really useful.
Sometimes the best answers come to questions you never thought of asking.
------------------------ Frank Caggiano frankcag at crystal-objects dot com http://www.crystal-objects.com
The best education for the best is the best education for all. Robert Maynard Hutchins
squeak-dev@lists.squeakfoundation.org