[ENH] Dictionary>>associationsSelect:

David T. Lewis lewis at mail.msen.com
Wed Feb 19 21:29:04 UTC 2003


Adds convenience method Dictionary>>associationsSelect, a variant of #select
which iterates over the associations rather than the values in a Dictionary.
This is useful for searching through dictionaries when both the keys and the
values are part of the select criteria.

A trivial test case is provided.

Dave

-------------- next part --------------
'From Squeak3.4gamma of ''7 January 2003'' [latest update: #5169] on 19 February 2003 at 4:33:13 pm'!
"Change Set:		AssociationsSelect-dtl
Date:			19 February 2003
Author:			David T. Lewis

Add convenience method Dictionary>>associationsSelect, a variant of #select which iterates over the associations rather than the values in a Dictionary."!


!Dictionary methodsFor: 'enumerating' stamp: 'dtl 2/17/2003 09:40'!
associationsSelect: aBlock 
	"Evaluate aBlock with each of my associations as the argument. Collect
	into a new dictionary, only those associations for which aBlock evaluates
	to true."

	| newCollection |
	newCollection _ self species new.
	self associationsDo: 
		[:each | 
		(aBlock value: each) ifTrue: [newCollection add: each]].
	^newCollection! !
-------------- next part --------------
'From Squeak3.4gamma of ''7 January 2003'' [latest update: #5169] on 19 February 2003 at 4:34:22 pm'!
"Change Set:		AssociationsSelectTestCase-dtl
Date:			19 February 2003
Author:			David T. Lewis

A trivial test case to demonstrate Dictionary>>associationsSelect:"!

TestCase subclass: #DictionaryAssociationsSelectTestCase
	instanceVariableNames: 'dictionary '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Unordered'!

!DictionaryAssociationsSelectTestCase commentStamp: 'dtl 2/19/2003 16:17' prior: 0!
A trivial test case to demonstrate Dictionary>>associationsSelect!


!DictionaryAssociationsSelectTestCase methodsFor: 'Running' stamp: 'dtl 2/19/2003 16:21'!
setUp

	dictionary _ Dictionary new.
	dictionary at: (Array with: #hello with: #world) put: #fooBar.
	dictionary at: Smalltalk put: #'Smalltalk is the key'.
	dictionary at: #Smalltalk put: Smalltalk! !

!DictionaryAssociationsSelectTestCase methodsFor: 'Testing' stamp: 'dtl 2/19/2003 16:27'!
testAss!
 ociationsSelect

	"(DictionaryAssociationsSelect selector: #testAssociationsSelect) run"

	| answer |
	answer _ dictionary associationsSelect:
		[:assoc | (assoc key == #Smalltalk) and: [assoc value == Smalltalk]].
	self should: [answer isKindOf: Dictionary].
	self should: [answer size == 1].
	self should: [(answer at: #Smalltalk) == Smalltalk]! !

!DictionaryAssociationsSelectTestCase methodsFor: 'Testing' stamp: 'dtl 2/19/2003 16:29'!
testAssociationsSelectEmptyDictionary

	"(DictionaryAssociationsSelectTestCase selector: #testAssociationsSelectEmptyDictionary) run"

	| answer |
	answer _ dictionary associationsSelect:
		[:assoc | (assoc key == #NoSuchKey) and: [assoc value == #NoSuchValue]].
	self should: [answer isKindOf: Dictionary].
	self should: [answer size == 0]
! !


More information about the Squeak-dev mailing list