gpt4 book ai didi

ruby - 枚举器的循环魔术(同时没有中断和无限循环)

转载 作者:数据小太阳 更新时间:2023-10-29 07:54:53 25 4
gpt4 key购买 nike

Enumerator.new 中的循环如何知道在哪里停止?

现在是更具描述性的例子。下面是两段代码,它们都返回相同的数组:[1,2,4,8]。但在第一个示例中,break 条件在 loop 中存在,当第二个示例以某种方式停止时。

带有 break 的示例 #1>

def simple n
x = []
a = 1
i = 0
loop do
x << a
a *= 2
i += 1
break unless i < n # in this case condition for stop used
end
x
end
p simple(4)== [1,2,4,8]

示例#2“神奇”

def enumer
Enumerator.new do |x|
a = 1
loop do # How do this loop know where to stop?
x << a
a *= 2
end
end
end
p enumer.take(4) == [1,2,4,8]

最佳答案

考虑以下几点:

enum = Enumerator.new do |x|
x << "hello"
x << "world"
end

enum.take(1)
#=> ["hello"]
enum.take(100)
#=> ["hello", "world"]

这是怎么回事?

嗯,产生的变量 x Enumerator::Yielder 的实例.每当您调用<<yield在变量上,一个值将附加到最终结果数组。

enum.take(n)是说“尝试为此可枚举收集最多 n 个值”。

因此,回顾您的原始示例,我们有:

loop do
x << a
a *= 2
end

因为你打给了take(4)在可枚举上,Enumerator::Yielder收藏了就知道马上返回4项目。

...另一方面,如果您尝试运行,例如enumer.to_a然后循环永远继续下去——因为它没有任何提前退出的条件!

根据我的发现,关于其工作原理的 ruby​​ 文档很少;但是有 this helpful description of the behaviour in the source code :

/*
* call-seq:
* Enumerator.new(size = nil) { |yielder| ... }
* Enumerator.new(obj, method = :each, *args)
*
* Creates a new Enumerator object, which can be used as an
* Enumerable.
*
* In the first form, iteration is defined by the given block, in
* which a "yielder" object, given as block parameter, can be used to
* yield a value by calling the +yield+ method (aliased as +<<+):
*
* fib = Enumerator.new do |y|
* a = b = 1
* loop do
* y << a
* a, b = b, a + b
* end
* end
*
* p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
*
* The optional parameter can be used to specify how to calculate the size
* in a lazy fashion (see Enumerator#size). It can either be a value or
* a callable object.
*
* In the second, deprecated, form, a generated Enumerator iterates over the
* given object using the given method with the given arguments passed.
*
* Use of this form is discouraged. Use Kernel#enum_for or Kernel#to_enum
* instead.
*
* e = Enumerator.new(ObjectSpace, :each_object)
* #-> ObjectSpace.enum_for(:each_object)
*
* e.select { |obj| obj.is_a?(Class) } #=> array of all classes
*
*/

关于ruby - 枚举器的循环魔术(同时没有中断和无限循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47680005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com