Custom Iterators
Copyright © 2007 by
In this article, I’ll discuss custom iterators, predicates, and generic methods. You can combine these three techniques to create small building blocks that plug together in a variety of ways to solve
everyday development problems. Once you can recognize when to apply this pattern, you will be able to create a large number of reusable building blocks.
When we build software, we rarely work with one instance of a data type. Instead, most of our work centers on collections of data: Lists,Arrays,Dictionaries, or other collections. As a result, so much of our code appears in a series of loops:
foreach ( Thing a in someContainer)
doWorkWith(a);
That’s simple enough, and there’s little point in trying to get more reuse from that simple construct. But most of our daily work isn’t really that simple. Sometimes the iteration creates a new collection, or a new sequence. Sometimes the iteration should only affect some of the elements in a collection, not all them. Sometimes the iteration will remove some elements from the collection. With all the different variations, we’re back to copying code and modifying it. That lowers our productivity. When you create custom iterators and predicates instead of copying and modifying loops, you decouple the common actions from the specific code. And, often, you will use the output from one iteration as the input to another. By using custom iterators to perform the iteration, you can save memory and increase performance as well.
Let’s start with a simple sample, and modify that code to create a set of reusable custom iterators and predicates. Suppose you’ve been asked to print out a list of people you know from the New York City phone book. It is a contrived problem, but it exhibits the characteristics of many real-world problems. You need to process a large number of objects (the list of all entries in the New York City phonebook). You need to extract a subset of the properties of each object (the first and last names). Finally, you need to filter the input set to generate the output set (only the people you know). Your first attempt might look like this:
// First attempt:
public List<string> PeopleIKnowInNewYork()
{
IEnumerable<PhoneBookEntrys> newYorkNumbers =
PhoneBook.FindListFor(“New York”);
List<string> peopleIKnow = new List<string>();
foreach ( PhoneBookEntry ph in newYorkNumbers)
{
string name = string.Format(“{0} {1}”, ph.FirstName, ph.LastName);
if ( RecognizePerson( name ) )
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS 百度文库 教育专区 资格考试/... In this article, I’ll discuss custom iterators_其它考试_资格考试/认证_...
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...Custom MQ implementation Financial applications in ...Ant Iterators File I/O JDBC I/O GroovyBeans ...
格式:pdf 关键词:70-536IT考试认证复习资料学习...14 Iterators ......(70-536) 5.3 Implement custom serialization formatting by using the Serialization...
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...invalidate iterators that point to existing elements...For Map custom objects you would need at the ...
格式:pdf 关键词:暂无1/2 相关文档推荐 References 55页 1财富值 references...We discuss several kinds of custom iterators for use with the STL that ...
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ... ArrayList class Collection interfaces Iterators Hash... and SemaphoreSecurity class Implement a custom ...
格式:pdf 关键词:计算机科学与技术 1/4 同系列...} // ERROR // Allow for custom comparison ...(StackKeys); } Iterators Want to be able to ...
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ... Iterators, and Algorithms Function Objects and ...custom covers and content particular to your ...
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...(访问值为空) Iterators (迭代器) Tracing (跟踪)...(CTF 级别) Custom Characters(自定义角色) 简介...
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...? The iterators of list will not be invalid ...Y N Y invoke OOM procedure custom-built the ...
热门文档