[Pharo-project] Collection extensions

Stéphane Ducasse stephane.ducasse at inria.fr
Mon Jan 5 08:30:25 EST 2009


Hi all

I published to the inbox some cool collection extension methods that  
we use all the time in Moose.
flatCollect:, collectAsSet:, and groupedBy:

http://code.google.com/p/pharo/issues/detail?id=432&colspec=ID%20Type%20Status%20Summary&start=100
I would like to have your feedback because we found them really useful  
in practice.

Here are some examples:

testFlatCollectArray
	"self debug: #testFlatCollectArray"
	
	self assert: ((#((1 2) (3 4) (5 3)) flatCollect: [ :each ]) = #(1 2 3  
4 5 3)).
	self assert: ((#((1 2) (2 3) (1 3 4)) flatCollect: [:each]) = #(1 2 2  
3 1 3 4)). 	

	self assert: ((#((1 2) (2 3) (1 3 4)) flatCollect: [:each| Set with:  
each])
					=  #(#(1 2) #(2 3) #(1 3 4))).


testFlatCollectSet
	"self debug: #testFlatCollectSet"
	
	self assert: ((#((1 2) (1 2) (1 3 4)) asSet  flatCollect: [:each]) =  
#(1 1 2 3 4) asSet).
	self assert: ((#() asSet flatCollect: [:each]) = #() asSet).

	self assert:  ((#((1 2) () (1 3 4)) asSet  flatCollect: [:each]) =  
#(1 1 2 3 4) asSet).
	self assert:  ((#((1 2) #((99)) (1 3 4)) asSet  flatCollect: [:each])
					= #(1 1 2 3 4 (99)) asSet).
	self assert:  ((#((1 2) #(()) (1 3 4)) asSet  flatCollect: [:each])
					= #(1 1 2 3 4 ()) asSet).			


testGroupedByArray
	"self debug: #testGroupedByArray"
	
	| res |
	res := #(1 2 3 4 5) groupedBy: [:each | each odd].
	self assert:   (res at: true) = #(1 3 5).
	self assert: (res at: false) = #(2 4)


testGroupedBy
	"self debug: #testGroupedBy"
	
	| res |
	res := #(1 2 3 4 5) asOrderedCollection
				groupedBy: [:each | each odd].
	self assert:   (res at: true) = #(1 3 5) asOrderedCollection.
	self assert: (res at: false) = #(2 4) asOrderedCollection





More information about the Pharo-dev mailing list