Block Closures (now Associations)

Ron Teitelbaum Ron at USMedRec.com
Tue Jan 10 17:56:28 UTC 2006


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 at USMedRec.com 

> -----Original Message-----
> From: squeak-dev-bounces at lists.squeakfoundation.org [mailto:squeak-dev-
> bounces at lists.squeakfoundation.org] On Behalf Of Joerg Beekmann
> Sent: Tuesday, January 10, 2006 11:06 AM
> To: Ron at 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 at deepcovelabs.com
> 
> 





More information about the Squeak-dev mailing list