class Enumerator::Lazy

Enumerator::LazyEnumerator 的一种特殊类型,它允许构建操作链,而无需立即求值,并按需评估值。为了做到这一点,它重写了 Enumerable 的大部分方法,使它们只构造另一个惰性枚举器。

可以使用 Enumerable#lazy 方法从任何 Enumerable 对象构造 Enumerator::Lazy

lazy = (1..Float::INFINITY).lazy.select(&:odd?).drop(10).take_while { |i| i < 30 }
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..Infinity>:select>:drop(10)>:take_while>

实际的枚举发生在调用任何非重写 Enumerable 方法时,例如 Enumerable#firstEnumerable#to_a (后者被别名为 force,以获得更具语义的代码)。

lazy.first(2)
#=> [21, 23]

lazy.force
#=> [21, 23, 25, 27, 29]

注意,大多数可以带或不带块调用的 Enumerable 方法,在 Enumerator::Lazy 上调用时,将始终需要一个块。

[1, 2, 3].map       #=> #<Enumerator: [1, 2, 3]:map>
[1, 2, 3].lazy.map  # ArgumentError: tried to call lazy map without a block

这个类允许对长序列或无限序列进行惯用的计算,以及在不构造中间数组的情况下链接计算。

处理缓慢计算序列的示例

require 'open-uri'

# This will fetch all URLs before selecting
# necessary data
URLS.map { |u| JSON.parse(URI.open(u).read) }
  .select { |data| data.key?('stats') }
  .first(5)

# This will fetch URLs one-by-one, only till
# there is enough data to satisfy the condition
URLS.lazy.map { |u| JSON.parse(URI.open(u).read) }
  .select { |data| data.key?('stats') }
  .first(5)

在链的末尾加上“.eager”会生成一个非惰性枚举器,适合返回或传递给另一个期望正常枚举器的类。

def active_items
  groups
    .lazy
    .flat_map(&:items)
    .reject(&:disabled)
    .eager
end

# This works lazily; if a checked item is found, it stops
# iteration and does not look into remaining groups.
first_checked = active_items.find(&:checked)

# This returns an array of items like a normal enumerator does.
all_checked = active_items.select(&:checked)