From 7dd52f895f8271be242ff549668d825295f12ccf Mon Sep 17 00:00:00 2001 From: shugo Date: Mon, 14 Jan 2013 08:36:15 +0000 Subject: [PATCH] * enumerator.c (lazy_flat_map_func): flat_map should call each only when the value of a block returns a forcable object. [ruby-core:51401] [Bug #7690] * enumerator.c (lazy_flat_map): add documentation. * test/ruby/test_lazy_enumerator.rb: related test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38812 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++ enumerator.c | 51 ++++++++++++++++++++++--------- test/ruby/test_lazy_enumerator.rb | 5 +++ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index d9b59ced07..313ad9e89b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Mon Jan 14 17:12:48 2013 Shugo Maeda + + * enumerator.c (lazy_flat_map_func): flat_map should call each only + when the value of a block returns a forcable object. + [ruby-core:51401] [Bug #7690] + + * enumerator.c (lazy_flat_map): add documentation. + + * test/ruby/test_lazy_enumerator.rb: related test. + Mon Jan 14 16:42:28 2013 Marc-Andre Lafortune * enumerator.c: Require block for Lazy#{take|drop}_while [Bug #7692] diff --git a/enumerator.c b/enumerator.c index 7a7cc1de83..b65712fe27 100644 --- a/enumerator.c +++ b/enumerator.c @@ -105,7 +105,7 @@ VALUE rb_cEnumerator; VALUE rb_cLazy; static ID id_rewind, id_each, id_new, id_initialize, id_yield, id_call, id_size; -static ID id_eqq, id_next, id_result, id_lazy, id_receiver, id_arguments, id_method; +static ID id_eqq, id_next, id_result, id_lazy, id_receiver, id_arguments, id_method, id_force; static VALUE sym_each, sym_cycle; VALUE rb_eStopIteration; @@ -1432,26 +1432,23 @@ lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv) } static VALUE -lazy_flat_map_each(VALUE obj) +lazy_flat_map_each(VALUE obj, VALUE yielder) { - NODE *memo = RNODE(obj); - rb_block_call(memo->u1.value, id_each, 0, 0, lazy_flat_map_i, - memo->u2.value); + rb_block_call(obj, id_each, 0, 0, lazy_flat_map_i, yielder); return Qnil; } static VALUE -lazy_flat_map_to_ary(VALUE obj) +lazy_flat_map_to_ary(VALUE obj, VALUE yielder) { - NODE *memo = RNODE(obj); - VALUE ary = rb_check_array_type(memo->u1.value); + VALUE ary = rb_check_array_type(obj); if (NIL_P(ary)) { - rb_funcall(memo->u2.value, id_yield, 1, memo->u1.value); + rb_funcall(yielder, id_yield, 1, obj); } else { long i; for (i = 0; i < RARRAY_LEN(ary); i++) { - rb_funcall(memo->u2.value, id_yield, 1, RARRAY_PTR(ary)[i]); + rb_funcall(yielder, id_yield, 1, RARRAY_PTR(ary)[i]); } } return Qnil; @@ -1468,15 +1465,38 @@ lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv) } } else { - NODE *memo; - memo = NEW_MEMO(result, argv[0], 0); - rb_rescue2(lazy_flat_map_each, (VALUE) memo, - lazy_flat_map_to_ary, (VALUE) memo, - rb_eNoMethodError, (VALUE)0); + if (rb_respond_to(result, id_force) && rb_respond_to(result, id_each)) { + lazy_flat_map_each(result, argv[0]); + } + else { + lazy_flat_map_to_ary(result, argv[0]); + } } return Qnil; } +/* + * call-seq: + * lazy.flat_map { |obj| block } -> a_lazy_enumerator + * + * Returns a new lazy enumerator with the concatenated results of running + * block once for every element in lazy. + * + * ["foo", "bar"].lazy.flat_map {|i| i.each_char.lazy}.force + * #=> ["f", "o", "o", "b", "a", "r"] + * + * A value x returned by block is decomposed if either of + * the following conditions is true: + * + * a) x responds to both each and force, which means that + * x is a lazy enumerator. + * b) x is an array or responds to to_ary. + * + * Otherwise, x is contained as-is in the return value. + * + * [{a:1}, {b:2}].lazy.flat_map {|i| i}.force + * #=> [{:a=>1}, {:b=>2}] + */ static VALUE lazy_flat_map(VALUE obj) { @@ -1936,6 +1956,7 @@ Init_Enumerator(void) id_receiver = rb_intern("receiver"); id_arguments = rb_intern("arguments"); id_method = rb_intern("method"); + id_force = rb_intern("force"); sym_each = ID2SYM(id_each); sym_cycle = ID2SYM(rb_intern("cycle")); diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index 30677291f4..f04d26ecbe 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -132,6 +132,11 @@ class TestLazyEnumerator < Test::Unit::TestCase assert_equal(["1", "2", "3"], [1, 2, 3].lazy.flat_map {|x| x.to_s}.force) end + def test_flat_map_hash + assert_equal([{?a=>97}, {?b=>98}, {?c=>99}], [?a, ?b, ?c].flat_map {|x| {x=>x.ord}}) + assert_equal([{?a=>97}, {?b=>98}, {?c=>99}], [?a, ?b, ?c].lazy.flat_map {|x| {x=>x.ord}}.force) + end + def test_reject a = Step.new(1..6) assert_equal(4, a.reject {|x| x < 4}.first)