mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enumerator.c (lazy_zip, lazy_cycle): Enumerator::Lazy#{zip,cycle}
should be eager when a block is given, to be consistent with Enumerable#{zip,cycle}. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ff1f6107f9
commit
032861ade7
3 changed files with 28 additions and 27 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Thu Mar 15 18:19:53 2012 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
|
* enumerator.c (lazy_zip, lazy_cycle): Enumerator::Lazy#{zip,cycle}
|
||||||
|
should be eager when a block is given, to be consistent with
|
||||||
|
Enumerable#{zip,cycle}.
|
||||||
|
|
||||||
Thu Mar 15 17:45:27 2012 Shugo Maeda <shugo@ruby-lang.org>
|
Thu Mar 15 17:45:27 2012 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
* enumerator.c (InitVM_Enumerator): renamed Enumerable::Lazy to
|
* enumerator.c (InitVM_Enumerator): renamed Enumerable::Lazy to
|
||||||
|
|
31
enumerator.c
31
enumerator.c
|
@ -1373,24 +1373,6 @@ lazy_grep(VALUE obj, VALUE pattern)
|
||||||
return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_grep_func, pattern);
|
return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_grep_func, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
|
||||||
lazy_zip_func_i(VALUE val, VALUE arg, int argc, VALUE *argv)
|
|
||||||
{
|
|
||||||
VALUE yielder, ary, v, result;
|
|
||||||
long i;
|
|
||||||
|
|
||||||
yielder = argv[0];
|
|
||||||
ary = rb_ary_new2(RARRAY_LEN(arg) + 1);
|
|
||||||
rb_ary_push(ary, argv[1]);
|
|
||||||
for (i = 0; i < RARRAY_LEN(arg); i++) {
|
|
||||||
v = rb_funcall(RARRAY_PTR(arg)[i], id_next, 0);
|
|
||||||
rb_ary_push(ary, v);
|
|
||||||
}
|
|
||||||
result = rb_yield(ary);
|
|
||||||
rb_funcall(yielder, id_yield, 1, result);
|
|
||||||
return Qnil;
|
|
||||||
}
|
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
lazy_zip_func(VALUE val, VALUE arg, int argc, VALUE *argv)
|
lazy_zip_func(VALUE val, VALUE arg, int argc, VALUE *argv)
|
||||||
{
|
{
|
||||||
|
@ -1414,14 +1396,15 @@ lazy_zip(int argc, VALUE *argv, VALUE obj)
|
||||||
VALUE ary;
|
VALUE ary;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (rb_block_given_p()) {
|
||||||
|
return rb_call_super(argc, argv);
|
||||||
|
}
|
||||||
ary = rb_ary_new2(argc);
|
ary = rb_ary_new2(argc);
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
rb_ary_push(ary, rb_funcall(argv[i], id_lazy, 0));
|
rb_ary_push(ary, rb_funcall(argv[i], id_lazy, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
return rb_block_call(rb_cLazy, id_new, 1, &obj,
|
return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_zip_func, ary);
|
||||||
rb_block_given_p() ? lazy_zip_func_i : lazy_zip_func,
|
|
||||||
ary);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -1528,6 +1511,9 @@ lazy_cycle(int argc, VALUE *argv, VALUE obj)
|
||||||
VALUE args;
|
VALUE args;
|
||||||
int len = rb_long2int((long)argc + 2);
|
int len = rb_long2int((long)argc + 2);
|
||||||
|
|
||||||
|
if (rb_block_given_p()) {
|
||||||
|
return rb_call_super(argc, argv);
|
||||||
|
}
|
||||||
args = rb_ary_tmp_new(len);
|
args = rb_ary_tmp_new(len);
|
||||||
rb_ary_push(args, obj);
|
rb_ary_push(args, obj);
|
||||||
rb_ary_push(args, sym_cycle);
|
rb_ary_push(args, sym_cycle);
|
||||||
|
@ -1535,8 +1521,7 @@ lazy_cycle(int argc, VALUE *argv, VALUE obj)
|
||||||
rb_ary_cat(args, argv, argc);
|
rb_ary_cat(args, argv, argc);
|
||||||
}
|
}
|
||||||
return rb_block_call(rb_cLazy, id_new, len, RARRAY_PTR(args),
|
return rb_block_call(rb_cLazy, id_new, len, RARRAY_PTR(args),
|
||||||
rb_block_given_p() ? lazy_map_func : lazy_cycle_func,
|
lazy_cycle_func, args /* prevent from GC */);
|
||||||
args /* prevent from GC */);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
|
|
@ -139,9 +139,12 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_zip_with_block
|
def test_zip_with_block
|
||||||
|
# zip should be eager when a block is given
|
||||||
a = Step.new(1..3)
|
a = Step.new(1..3)
|
||||||
assert_equal(["a", 1], a.lazy.zip("a".."c") {|x, y| [y, x]}.first)
|
ary = []
|
||||||
assert_equal(1, a.current)
|
assert_equal(nil, a.lazy.zip("a".."c") {|x, y| ary << [x, y]})
|
||||||
|
assert_equal(a.zip("a".."c"), ary)
|
||||||
|
assert_equal(3, a.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_take
|
def test_take
|
||||||
|
@ -190,8 +193,15 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
||||||
assert_equal(3, a.current)
|
assert_equal(3, a.current)
|
||||||
assert_equal("1", a.lazy.cycle(2).map(&:to_s).first)
|
assert_equal("1", a.lazy.cycle(2).map(&:to_s).first)
|
||||||
assert_equal(1, a.current)
|
assert_equal(1, a.current)
|
||||||
assert_equal("1", a.lazy.cycle(2, &:to_s).first)
|
end
|
||||||
assert_equal(1, a.current)
|
|
||||||
|
def test_cycle_with_block
|
||||||
|
# cycle should be eager when a block is given
|
||||||
|
a = Step.new(1..3)
|
||||||
|
ary = []
|
||||||
|
assert_equal(nil, a.lazy.cycle(2) {|i| ary << i})
|
||||||
|
assert_equal(a.cycle(2).to_a, ary)
|
||||||
|
assert_equal(3, a.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_force
|
def test_force
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue