ruby-changes:22955
From: shugo <ko1@a...>
Date: Wed, 14 Mar 2012 08:16:46 +0900 (JST)
Subject: [ruby-changes:22955] shugo:r35004 (trunk): * enumerator.c (lazy_zip_func): use each for non-Array objects.
shugo 2012-03-14 08:16:37 +0900 (Wed, 14 Mar 2012) New Revision: 35004 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35004 Log: * enumerator.c (lazy_zip_func): use each for non-Array objects. Modified files: trunk/ChangeLog trunk/enumerator.c trunk/test/ruby/test_lazy_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35003) +++ ChangeLog (revision 35004) @@ -1,3 +1,7 @@ +Wed Mar 14 08:15:54 2012 Shugo Maeda <shugo@r...> + + * enumerator.c (lazy_zip_func): use each for non-Array objects. + Wed Mar 14 08:06:35 2012 Shugo Maeda <shugo@r...> * enumerator.c (lazy_zip): add Enumerable::Lazy#flat_map. Index: enumerator.c =================================================================== --- enumerator.c (revision 35003) +++ enumerator.c (revision 35004) @@ -1294,20 +1294,25 @@ } 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); - } - else { + if (TYPE(result) == T_ARRAY) { int i; - for (i = 0; i < RARRAY_LEN(ary); i++) { - rb_funcall(argv[0], id_yield, 1, RARRAY_PTR(ary)[i]); + for (i = 0; i < RARRAY_LEN(result); i++) { + rb_funcall(argv[0], id_yield, 1, RARRAY_PTR(result)[i]); } - return Qnil; } + else { + rb_block_call(result, id_each, 0, 0, lazy_flat_map_i, argv[0]); + } + return Qnil; } static VALUE Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 35003) +++ test/ruby/test_lazy_enumerator.rb (revision 35004) @@ -88,6 +88,16 @@ 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) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/