文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 资格考试/认证 > 其它考试 > custom-iteratorspdf
侵权投诉

custom-iteratorspdf

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 ) )

第1页

你可能喜欢

热门文档

相关文档

  • In this article, I’ll discuss custom iterators

    搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS 百度文库 教育专区 资格考试/... In this article, I’ll discuss custom iterators_其它考试_资格考试/认证_...

  • BarCampKC-2008-Intro-To-Groovy

    搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...Custom MQ implementation Financial applications in ...Ant Iterators File I/O JDBC I/O GroovyBeans ...

  • 70-536_Free_Study_Guide

    格式:pdf 关键词:70-536IT考试认证复习资料学习...14 Iterators ......(70-536) 5.3 Implement custom serialization formatting by using the Serialization...

  • hash_map函数

    搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...invalidate iterators that point to existing elements...For Map custom objects you would need at the ...

  • References

    格式:pdf 关键词:暂无1/2 相关文档推荐 References 55页 1财富值 references...We discuss several kinds of custom iterators for use with the STL that ...

  • 70-551资料

    搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ... ArrayList class Collection interfaces Iterators Hash... and SemaphoreSecurity class Implement a custom ...

  • 19-FuncDataMappingIter

    格式:pdf 关键词:计算机科学与技术 1/4 同系列...} // ERROR // Allow for custom comparison ...(StackKeys); } Iterators Want to be able to ...

  • Beyond the C++ Standard Library

    搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ... Iterators, and Algorithms Function Objects and ...custom covers and content particular to your ...

  • ut3制作

    搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...(访问值为空) Iterators (迭代器) Tracing (跟踪)...(CTF 级别) Custom Characters(自定义角色) 简介...

  • research of list-new

    搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ...? The iterators of list will not be invalid ...Y N Y invoke OOM procedure custom-built the ...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email