在ruby1.9中增加了External Iterators这中新的迭代类型,所有的Enumerable 都是External Iterators.而在ruby1.9之前的版本中的迭代都是internal iterator。
何为external iterator呢,其实我认为也就是一个生成器了:
iterator = 9.downto(1) # An enumerator as external iterator
begin # So we can use rescue below
print iterator.next while true # Call the next method repeatedly
rescue StopIteration # When there are no more values
puts "...blastoff!" # An expected, nonexceptional condition
end
这里的Kernel.loop方法就是为了迭代External Iterators而专门创建的。当next方法到最后一个元素的时候,就会抛出StopIteration,因此这里我们捕捉这个异常来判断迭代是否结束.
任何External Iterators都可以调用rewind 方法来重启这个迭代器。要注意如果一个enumerator 基于像File这样的对象,那么调用rewind将不会起作用。
一旦一个External Iterators开始迭代,那么他就不能被clone和duplicated。
我们可以简单的使用External Iterators来创建一个each方法,也就是通过External Iterator来创建一个internal iterator:
module Iterable
include Enumerable # Define iterators on top of each
def each # And define each on top of next
loop { yield self.next }
end
end
下面我们可以这样做:
def iterate(iterator)
loop { yield iterator.next }
end
iterate(9.downto(1)) {|x| print x }
下面是三个函数,功能都是一样的,第一个使用internal iterators,第二个虽然使用了External iterators ,可是没有用loop方法,因此代码很丑陋,第三个方法是最好的方法。:
# Call the each method of each collection in turn.
# This is not a parallel iteration and does not require enumerators.
def sequence(*enumerables, &block)
enumerables.each do |enumerable|
enumerable.each(&block)
end
end
# Iterate the specified collections, interleaving their elements.
# This can't be done efficiently without external iterators.
# Note the use of the uncommon else clause in begin/rescue.
def interleave(*enumerables)
# Convert enumerable collections to an array of enumerators.
enumerators = enumerables.map {|e| e.to_enum }
# Loop until we don't have any more enumerators.
until enumerators.empty?
begin
e = enumerators.shift # Take the first enumerator
yield e.next # Get its next and pass to the block
rescue StopIteration # If no more elements, do nothing
else # If no exception occurred
enumerators << e # Put the enumerator back
end
end
end
# Iterate the specified collections, yielding tuples of values,
# one value from each of the collections. See also Enumerable.zip.
def bundle(*enumerables)
enumerators = enumerables.map {|e| e.to_enum }
loop { yield enumerators.map {|e| e.next} }
end
# Examples of how these iterator methods work
a,b,c = [1,2,3], 4..6, 'a'..'e'
sequence(a,b,c) {|x| print x} # prints "123456abcde"
interleave(a,b,c) {|x| print x} # prints "14a25b36cde"
bundle(a,b,c) {|x| print x} # '[1, 4, "a"][2, 5, "b"][3, 6, "c"]'
这里要注意,在ruby1.8中的zip效率很低,那是因为它首先转换参数为数组,然后迭代他,可是在1.9中,由于我们有了External Iterators,因此它的实现就是直接迭代。因此他就更高效.
分享到:
相关推荐
js-iterators(零依赖) JS中的Ruby样式迭代器。 JS中的Ruby样式范围[ ... 1. . _10 ] ; // [1, 2, 3, 4, 5, 6, 7, 8, 9][ ... 1. . _ - 10 ] ; // [1, 0, -1, -2, ..., -9][ ... Math . PI . _9 ] ; // [3....
赠送jar包:deeplearning4j-utility-iterators-1.0.0-M1.1.jar; 赠送原API文档:deeplearning4j-utility-iterators-1.0.0-M1.1-javadoc.jar; 赠送源代码:deeplearning4j-utility-iterators-1.0.0-M1.1-sources....
12-iterators-and-generators(迭代器和生成器12).pdf
赠送jar包:deeplearning4j-datavec-iterators-1.0.0-M1.1.jar; 赠送原API文档:deeplearning4j-datavec-iterators-1.0.0-M1.1-javadoc.jar; 赠送源代码:deeplearning4j-datavec-iterators-1.0.0-M1.1-sources....
赠送jar包:deeplearning4j-datavec-iterators-1.0.0-M1.1.jar; 赠送原API文档:deeplearning4j-datavec-iterators-1.0.0-M1.1-javadoc.jar; 赠送源代码:deeplearning4j-datavec-iterators-1.0.0-M1.1-sources....
赠送jar包:deeplearning4j-utility-iterators-1.0.0-M1.1.jar; 赠送原API文档:deeplearning4j-utility-iterators-1.0.0-M1.1-javadoc.jar; 赠送源代码:deeplearning4j-utility-iterators-1.0.0-M1.1-sources....
Work with arrays, ranges, hashes, and iterators Handle dates and time Process XML as text Master OOP concepts such as classes, objects, subclassing, and inheritance Connect to databases Who This Book ...
注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【*...
Java Methods-Lists and Iterators.ppt
泛型指標(Iterators)與Traits 技術.zip
Extended STL, Volume 1 - Collections and Iterators 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,...
注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【*...
Iterators 是 JS中的新方法,可以用来循环任意集合。 在ES6中登场的Iterators。因其可被广泛使用,并且已在多处场景派上用场,我们将从概念上理解迭代器是什么,以及在何处使用它们和示例。我们还将看到它在JS 中的...
迭代器 使用的代码 安装: $ git clone ...要执行它们中的每一个,您必须使用以下命令: $ npm run babel-node -- [class | native | object | generators].js 玩一下例子,看看你能走多远。
arvores-iterators:Estudo
在迭代器实验室中,我们将继续探索迭代器。 我们面临一系列挑战,需要您使用我们在课堂上讨论过的迭代器。 我们将尝试使用各种测试方法来验证您的代码是否正常工作。 阶段1 研究以下术语并用两到三句话总结你对它的...
at42ft_containers:重新实现STL中的某些容器(矢量,列表,地图,堆栈,队列和&及其迭代器,const_iterators,reverse_iterators)[C ++]
如何编译和运行示例代码: git clone https://github.com/rustomax/rust-iterators.gitcd rust-iterators/cargo run内容介绍生活是重复性的,生活中的大多数事物都是一系列物品。 以编程方式,我们经常需要对这些...