1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* enumerator.c (lazy_zip_func): use each for non-Array objects.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35004 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2012-03-13 23:16:37 +00:00
parent 22434473c7
commit 6b4525912b
3 changed files with 27 additions and 8 deletions

View file

@ -1,3 +1,7 @@
Wed Mar 14 08:15:54 2012 Shugo Maeda <shugo@ruby-lang.org>
* enumerator.c (lazy_zip_func): use each for non-Array objects.
Wed Mar 14 08:06:35 2012 Shugo Maeda <shugo@ruby-lang.org>
* enumerator.c (lazy_zip): add Enumerable::Lazy#flat_map.

View file

@ -1293,21 +1293,26 @@ lazy_map(VALUE obj)
return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_map_func, 0);
}
static VALUE
lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv)
{
return rb_funcall2(yielder, id_yield, argc, argv);
}
static VALUE
lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
{
VALUE result = rb_yield_values2(argc - 1, &argv[1]);
VALUE ary = rb_check_array_type(result);
if (NIL_P(ary)) {
return rb_funcall(argv[0], id_yield, 1, result);
if (TYPE(result) == T_ARRAY) {
int i;
for (i = 0; i < RARRAY_LEN(result); i++) {
rb_funcall(argv[0], id_yield, 1, RARRAY_PTR(result)[i]);
}
}
else {
int i;
for (i = 0; i < RARRAY_LEN(ary); i++) {
rb_funcall(argv[0], id_yield, 1, RARRAY_PTR(ary)[i]);
}
return Qnil;
rb_block_call(result, id_each, 0, 0, lazy_flat_map_i, argv[0]);
}
return Qnil;
}
static VALUE

View file

@ -88,6 +88,16 @@ class TestLazyEnumerator < Test::Unit::TestCase
assert_equal(1, a.current)
end
def test_flat_map_nested
a = Step.new(1..3)
assert_equal([1, "a"],
a.flat_map {|x| ("a".."c").map {|y| [x, y]}}.first)
assert_equal(3, a.current)
assert_equal([1, "a"],
a.lazy.flat_map {|x| ("a".."c").lazy.map {|y| [x, y]}}.first)
assert_equal(1, a.current)
end
def test_reject
a = Step.new(1..6)
assert_equal(4, a.reject {|x| x < 4}.first)