This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Replacing Values in Collections
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
Both the post title AND content made me think about another form of the same. When you want to replace all of the existing elements in a collection given a transform expressed by a block. It is interesting to look at some use cases of collect:. Often there is an initial collection. And then there's a transformation or two that takes place using collect: Each results in a new collection being created. Often though, they're just transient. For most cases, the difference between creating a new collection for the transformation and reusing the old ones, is academic. In the case of large collections, it can become a serious issue.
Whether it's just the academic or the performance that motivates you, you soon decide to exten your image with something like:
SequenceableCollection>>modify: aBlock
1 to: self size do: [:i | self at: i put: (aBlock value: (self at: i))]
You can of course do the analog for Dictionaries. With this method you can now modify a collection in place:
customerNames := #('james_bond' 'me_too' 'him_and_me').
customerNames modify: [:each | each tokensBasedOn: %_].
customerNames modify: [:each | each modify: [:name | name raiseFirst]]
It's not a very good example, but it works to illustrate. I found soon after doing modify:, that I wanted a with:modify:, analagous to the with:collect: methods that have been added to the system. For some actual good examples of (with:)modify:, one can load and look at NumericCollections, which uses these to make possible something like:
#(1 2 3 4 5 6) += 5
The NumericCollections version is actually named (with:)numericModify: to separate it from our own BaseExtensions package.
David demonstrated a code pattern inspired by a simple pattern in C. In this case, I'd like to C(++) come up with the pattern that emulates this one.