类 Enumerator::Lazy
Enumerator::Lazy 是一种特殊的 Enumerator 类型,它允许构造操作链而不立即计算它们,并且根据需要计算值。为此,它重新定义了大多数 Enumerable 方法,使它们只构造另一个惰性枚举器。
Enumerator::Lazy 可以从任何 Enumerable 中使用 Enumerable#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#first 或 Enumerable#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)
公共类方法
源代码
static VALUE
lazy_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE obj, size = Qnil;
VALUE generator;
rb_check_arity(argc, 1, 2);
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy new without a block");
}
obj = argv[0];
if (argc > 1) {
size = argv[1];
}
generator = generator_allocate(rb_cGenerator);
rb_block_call(generator, id_initialize, 0, 0, lazy_init_block_i, obj);
enumerator_init(self, generator, sym_each, 0, 0, 0, size, 0);
rb_ivar_set(self, id_receiver, obj);
return self;
}
创建一个新的 Lazy 枚举器。当实际枚举枚举器时(例如,通过调用 force),将枚举 obj 并且每个值都将传递给给定的块。该块可以使用 yielder 将值返回。例如,要创建一个“filter+map”枚举器
def filter_map(sequence) Lazy.new(sequence) do |yielder, *values| result = yield *values yielder << result if result end end filter_map(1..Float::INFINITY) {|i| i*i if i.even?}.first(5) #=> [4, 16, 36, 64, 100]
公共实例方法
类似于 Enumerable#map,但将操作链接为惰性计算。
(1..Float::INFINITY).lazy.map {|i| i**2 } #=> #<Enumerator::Lazy: #<Enumerator::Lazy: 1..Infinity>:map> (1..Float::INFINITY).lazy.map {|i| i**2 }.first(3) #=> [1, 4, 9]
返回一个新的惰性枚举器,其中包含对惰性枚举器中的每个元素运行一次 block 的连接结果。
["foo", "bar"].lazy.flat_map {|i| i.each_char.lazy}.force #=> ["f", "o", "o", "b", "a", "r"]
如果以下任一条件为真,则会分解由 block 返回的值 x
-
x响应 each 和 force,这意味着x是一个惰性枚举器。 -
x是一个数组或响应 to_ary。
否则,x 将按原样包含在返回值中。
[{a:1}, {b:2}].lazy.flat_map {|i| i}.force
#=> [{:a=>1}, {:b=>2}]
源代码
endif
static VALUE
lazy_super(int argc, VALUE *argv, VALUE lazy)
{
return enumerable_lazy(rb_call_super(argc, argv));
}
类似于 Enumerable#chunk,但将操作链接为惰性计算。
类似于 Enumerable#map,但将操作链接为惰性计算。
(1..Float::INFINITY).lazy.map {|i| i**2 } #=> #<Enumerator::Lazy: #<Enumerator::Lazy: 1..Infinity>:map> (1..Float::INFINITY).lazy.map {|i| i**2 }.first(3) #=> [1, 4, 9]
返回一个新的惰性枚举器,其中包含对惰性枚举器中的每个元素运行一次 block 的连接结果。
["foo", "bar"].lazy.flat_map {|i| i.each_char.lazy}.force #=> ["f", "o", "o", "b", "a", "r"]
如果以下任一条件为真,则会分解由 block 返回的值 x
-
x响应 each 和 force,这意味着x是一个惰性枚举器。 -
x是一个数组或响应 to_ary。
否则,x 将按原样包含在返回值中。
[{a:1}, {b:2}].lazy.flat_map {|i| i}.force
#=> [{:a=>1}, {:b=>2}]
源代码
static VALUE
lazy_compact(VALUE obj)
{
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_compact_funcs);
}
类似于 Enumerable#compact,但将操作链接为惰性计算。
源代码
static VALUE
lazy_drop(VALUE obj, VALUE n)
{
long len = NUM2LONG(n);
VALUE argv[2];
argv[0] = sym_each;
argv[1] = n;
if (len < 0) {
rb_raise(rb_eArgError, "attempt to drop negative size");
}
return lazy_add_method(obj, 2, argv, n, rb_ary_new3(1, n), &lazy_drop_funcs);
}
类似于 Enumerable#drop,但将操作链接为惰性计算。
源代码
static VALUE
lazy_drop_while(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy drop_while without a block");
}
return lazy_add_method(obj, 0, 0, Qfalse, Qnil, &lazy_drop_while_funcs);
}
类似于 Enumerable#drop_while,但将操作链接为惰性计算。
源代码
static VALUE
lazy_eager(VALUE self)
{
return enumerator_init(enumerator_allocate(rb_cEnumerator),
self, sym_each, 0, 0, lazy_eager_size, Qnil, 0);
}
返回从惰性枚举器转换的非惰性 Enumerator。
类似于 Object#to_enum,但它返回一个惰性枚举器。这使得定义 Enumerable 方法变得容易,如果从惰性枚举器调用它们,它们自然会保持惰性。
例如,继续 Object#to_enum 中的示例
# See Object#to_enum for the definition of repeat r = 1..Float::INFINITY r.repeat(2).first(5) # => [1, 1, 2, 2, 3] r.repeat(2).class # => Enumerator r.repeat(2).map{|n| n ** 2}.first(5) # => endless loop! # works naturally on lazy enumerator: r.lazy.repeat(2).class # => Enumerator::Lazy r.lazy.repeat(2).map{|n| n ** 2}.first(5) # => [1, 1, 4, 4, 9]
源代码
static VALUE
lazy_filter_map(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy filter_map without a block");
}
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_filter_map_funcs);
}
类似于 Enumerable#filter_map,但将操作链接为惰性计算。
(1..).lazy.filter_map { |i| i * 2 if i.even? }.first(5) #=> [4, 8, 12, 16, 20]
源代码
static VALUE
lazy_flat_map(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy flat_map without a block");
}
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_flat_map_funcs);
}
返回一个新的惰性枚举器,其中包含对惰性枚举器中的每个元素运行一次 block 的连接结果。
["foo", "bar"].lazy.flat_map {|i| i.each_char.lazy}.force #=> ["f", "o", "o", "b", "a", "r"]
如果以下任一条件为真,则会分解由 block 返回的值 x
-
x响应 each 和 force,这意味着x是一个惰性枚举器。 -
x是一个数组或响应 to_ary。
否则,x 将按原样包含在返回值中。
[{a:1}, {b:2}].lazy.flat_map {|i| i}.force
#=> [{:a=>1}, {:b=>2}]
源代码
static VALUE
lazy_grep(VALUE obj, VALUE pattern)
{
const lazyenum_funcs *const funcs = rb_block_given_p() ?
&lazy_grep_iter_funcs : &lazy_grep_funcs;
return lazy_add_method(obj, 0, 0, pattern, rb_ary_new3(1, pattern), funcs);
}
类似于 Enumerable#grep,但将操作链接为惰性计算。
源代码
static VALUE
lazy_grep_v(VALUE obj, VALUE pattern)
{
const lazyenum_funcs *const funcs = rb_block_given_p() ?
&lazy_grep_v_iter_funcs : &lazy_grep_v_funcs;
return lazy_add_method(obj, 0, 0, pattern, rb_ary_new3(1, pattern), funcs);
}
类似于 Enumerable#grep_v,但将操作链接为惰性计算。
源代码
static VALUE
lazy_map(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy map without a block");
}
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_map_funcs);
}
类似于 Enumerable#map,但将操作链接为惰性计算。
(1..Float::INFINITY).lazy.map {|i| i**2 } #=> #<Enumerator::Lazy: #<Enumerator::Lazy: 1..Infinity>:map> (1..Float::INFINITY).lazy.map {|i| i**2 }.first(3) #=> [1, 4, 9]
源代码
static VALUE
lazy_reject(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy reject without a block");
}
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_reject_funcs);
}
类似于 Enumerable#reject,但将操作链接为惰性计算。
源代码
static VALUE
lazy_select(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy select without a block");
}
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_select_funcs);
}
类似于 Enumerable#select,但将操作链接为惰性计算。
源代码
static VALUE
lazy_take(VALUE obj, VALUE n)
{
long len = NUM2LONG(n);
if (len < 0) {
rb_raise(rb_eArgError, "attempt to take negative size");
}
n = LONG2NUM(len); /* no more conversion */
return lazy_add_method(obj, 0, 0, n, rb_ary_new3(1, n), &lazy_take_funcs);
}
类似于 Enumerable#take,但将操作链接为惰性计算。
源代码
static VALUE
lazy_take_while(VALUE obj)
{
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "tried to call lazy take_while without a block");
}
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_take_while_funcs);
}
类似于 Enumerable#take_while,但将操作链接为惰性计算。
源代码
static VALUE
lazy_to_enum(int argc, VALUE *argv, VALUE self)
{
VALUE lazy, meth = sym_each, super_meth;
if (argc > 0) {
--argc;
meth = *argv++;
}
if (RTEST((super_meth = rb_hash_aref(lazy_use_super_method, meth)))) {
meth = super_meth;
}
lazy = lazy_to_enum_i(self, meth, argc, argv, 0, rb_keyword_given_p());
if (rb_block_given_p()) {
RB_OBJ_WRITE(lazy, &enumerator_ptr(lazy)->size, rb_block_proc());
}
return lazy;
}
类似于 Object#to_enum,但它返回一个惰性枚举器。这使得定义 Enumerable 方法变得容易,如果从惰性枚举器调用它们,它们自然会保持惰性。
例如,继续 Object#to_enum 中的示例
# See Object#to_enum for the definition of repeat r = 1..Float::INFINITY r.repeat(2).first(5) # => [1, 1, 2, 2, 3] r.repeat(2).class # => Enumerator r.repeat(2).map{|n| n ** 2}.first(5) # => endless loop! # works naturally on lazy enumerator: r.lazy.repeat(2).class # => Enumerator::Lazy r.lazy.repeat(2).map{|n| n ** 2}.first(5) # => [1, 1, 4, 4, 9]
源代码
static VALUE
lazy_uniq(VALUE obj)
{
const lazyenum_funcs *const funcs =
rb_block_given_p() ? &lazy_uniq_iter_funcs : &lazy_uniq_funcs;
return lazy_add_method(obj, 0, 0, Qnil, Qnil, funcs);
}
类似于 Enumerable#uniq,但将操作链接为惰性计算。
源代码
static VALUE
lazy_with_index(int argc, VALUE *argv, VALUE obj)
{
VALUE memo;
rb_scan_args(argc, argv, "01", &memo);
if (NIL_P(memo))
memo = LONG2NUM(0);
return lazy_add_method(obj, 0, 0, memo, rb_ary_new_from_values(1, &memo), &lazy_with_index_funcs);
}
如果给定了代码块,则返回一个惰性枚举器,该枚举器将使用从 offset 开始的索引对每个元素迭代给定的代码块,并返回一个惰性枚举器,该枚举器产生相同的值(不带索引)。
如果未给定代码块,则返回一个新的惰性枚举器,该枚举器包含从 offset 开始的索引。
offset-
要使用的起始索引
源代码
static VALUE
lazy_zip(int argc, VALUE *argv, VALUE obj)
{
VALUE ary, v;
long i;
const lazyenum_funcs *funcs = &lazy_zip_funcs[1];
if (rb_block_given_p()) {
return rb_call_super(argc, argv);
}
ary = rb_ary_new2(argc);
for (i = 0; i < argc; i++) {
v = rb_check_array_type(argv[i]);
if (NIL_P(v)) {
for (; i < argc; i++) {
if (!rb_respond_to(argv[i], id_each)) {
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
rb_obj_class(argv[i]));
}
}
ary = rb_ary_new4(argc, argv);
funcs = &lazy_zip_funcs[0];
break;
}
rb_ary_push(ary, v);
}
return lazy_add_method(obj, 0, 0, ary, ary, funcs);
}
类似于 Enumerable#zip,但将操作链接为惰性求值。但是,如果给 zip 提供了代码块,则会立即枚举值。
私有实例方法
源代码
static VALUE
enumerator_with_index(int argc, VALUE *argv, VALUE obj)
{
VALUE memo;
rb_check_arity(argc, 0, 1);
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_enum_size);
memo = (!argc || NIL_P(memo = argv[0])) ? INT2FIX(0) : rb_to_int(memo);
return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)MEMO_NEW(memo, 0, 0));
}
使用从 offset 开始的索引对每个元素迭代给定的代码块。如果没有提供代码块,则返回一个新的Enumerator,该枚举器包含从 offset 开始的索引。
offset-
要使用的起始索引