FogBugz (Caso [Issue]12249) Collection - Useful collection extensions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

FogBugz (Caso [Issue]12249) Collection - Useful collection extensions

Pharo Issue Tracker
FogBugz Notification
avatar
gcotelli opened Case 12249: Useful collection extensions and assigned it to gcotelli:
Enhancement in Project:  Collection: 1. Pharo Image  •  You are subscribed to this case
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 Priority: 4 – Would be nice Status Status: Work Needed
Assigned To Assigned to: gcotelli Milestone Milestone: Later

Go to Case
No longer need updates? Unsubscribe from this case.

Don't want FogBugz notifications anymore? Update your preferences.

FogBugz

_______________________________________________
Pharo-bugtracker mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-bugtracker