The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Replacing Values in Collections

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Replacing Values in Collections Posted: Jan 21, 2005 2:02 PM
Reply to this message Reply

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
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Travis Griggs - Blog

Advertisement
David Buck had an interesting post called... Replacing values in collections. Cool stuff.

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.

Read: Replacing Values in Collections

Topic: Editing the web, one page at a time Previous Topic   Next Topic Topic: An unanticipated use for ExtraEmphases

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use