|
Implement two useful extensions in Collection:
detect:ifFound:
detect:ifFound:ifNone:
This extensions helps to avoid code like this (doing isNil checks or using some object as a sentinel to check against):
<code>selectHandsToDrawForDamage: damageList
"Select the set of hands that must be redrawn because either (a) the hand itself has changed or (b) the hand intersects some damage rectangle."
| result |
result := OrderedCollection new.
hands do: [:h | | hBnds |
h needsToBeDrawn ifTrue: [
h hasChanged
ifTrue: [result add: h]
ifFalse: [
hBnds := h fullBounds.
(damageList detect: [:r | r intersects: hBnds] ifNone: [nil])
ifNotNil: [result add: h]]]].
^ result</code>
turns into:
<code>selectHandsToDrawForDamage: damageList
"Select the set of hands that must be redrawn because either (a) the hand itself has changed or (b) the hand intersects some damage rectangle."
| result |
result := OrderedCollection new.
hands do: [:h | | hBnds |
h needsToBeDrawn ifTrue: [
h hasChanged
ifTrue: [result add: h]
ifFalse: [
hBnds := h fullBounds.
damageList detect: [:r | r intersects: hBnds] ifFound: [:r | result add: h]]]].
^ result </code>
or
<code>removeAlarm: aSelector for: aTarget
"Remove the alarm with the given selector"
| alarm |
alarm := self alarms
detect: [:any | any receiver == aTarget and: [any selector == aSelector]]
ifNone: [nil].
alarm ifNotNil: [self alarms remove: alarm]</code>
becomes:
<code>removeAlarm: aSelector for: aTarget
"Remove the alarm with the given selector"
self alarms
detect: [:any | any receiver == aTarget and: [any selector == aSelector]]
ifFound: [:alarm | self alarms remove: alarm].</code>
one with ifNone:
<code>saveContentsInFile
"Save the receiver's contents string to a file, prompting the user for a file-name. Suggest a reasonable file-name."
| fileName stringToSave parentWindow labelToUse suggestedName lastIndex |
stringToSave := self string.
stringToSave size = 0 ifTrue: [^self inform: 'nothing to save.'].
parentWindow := model dependents
detect: [:dep | dep isKindOf: SystemWindow]
ifNone: [nil].
labelToUse := parentWindow ifNil: ['Untitled']
ifNotNil: [parentWindow label].
...</code>
becomes:
<code>saveContentsInFile
"Save the receiver's contents string to a file, prompting the user for a file-name. Suggest a reasonable file-name."
| fileName stringToSave labelToUse suggestedName lastIndex |
stringToSave := self string.
stringToSave size = 0 ifTrue: [^self inform: 'nothing to save.'].
labelToUse := model dependents
detect: [:dep | dep isKindOf: SystemWindow]
ifFound: [:parentWindow | parentWindow label]
ifNone: ['Untitled'].
...</code>
|
|
|
Priority: 4 – Would be nice
|
|
Status: Work Needed
|
|
Assigned to: gcotelli
|
|
Milestone: Later
|
Go to Case
|
|