ruby-changes:5296
From: knu <ko1@a...>
Date: Tue, 3 Jun 2008 21:44:02 +0900 (JST)
Subject: [ruby-changes:5296] Ruby:r16795 (trunk): * enumerator.c (enumerator_with_memo): New method: with_memo().
knu 2008-06-03 21:43:45 +0900 (Tue, 03 Jun 2008) New Revision: 16795 Modified files: trunk/ChangeLog trunk/doc/NEWS trunk/enumerator.c trunk/test/ruby/test_enumerator.rb Log: * enumerator.c (enumerator_with_memo): New method: with_memo(). http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16795&r2=16794&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_enumerator.rb?r1=16795&r2=16794&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/enumerator.c?r1=16795&r2=16794&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/doc/NEWS?r1=16795&r2=16794&diff_format=u Index: doc/NEWS =================================================================== --- doc/NEWS (revision 16794) +++ doc/NEWS (revision 16795) @@ -107,6 +107,7 @@ * Enumerable and Enumerator o Enumerable#map,collect_all called without a block returns an enumerator. + o Enumerable::Enumerator#with_memo * Regexp#match, String#match o Regexp#match, String#match * Fiber: coroutines/micro-threads Index: ChangeLog =================================================================== --- ChangeLog (revision 16794) +++ ChangeLog (revision 16795) @@ -1,3 +1,7 @@ +Tue Jun 3 19:35:02 2008 Akinori MUSHA <knu@i...> + + * enumerator.c (enumerator_with_memo): New method: with_memo(). + Tue Jun 3 20:04:03 2008 Nobuyoshi Nakada <nobu@r...> * win32/Makefile.sub (miniruby$(EXEEXT)): miniruby cannot be Index: enumerator.c =================================================================== --- enumerator.c (revision 16794) +++ enumerator.c (revision 16795) @@ -358,6 +358,42 @@ } static VALUE +enumerator_with_memo_i(VALUE val, VALUE memo) +{ + return rb_yield_values(2, val, memo); +} + +/* + * call-seq: + * e.with_memo(memo) {|(*args), memo| ... } + * e.with_memo(memo) + * + * Iterates the given block for each elements with an arbitrary + * object given, and returns the memo object. + * + * If no block is given, returns an enumerator. + * + */ +static VALUE +enumerator_with_memo(VALUE obj, VALUE memo) +{ + struct enumerator *e; + int argc = 0; + VALUE *argv = 0; + + RETURN_ENUMERATOR(obj, 0, 0); + e = enumerator_ptr(obj); + if (e->args) { + argc = RARRAY_LEN(e->args); + argv = RARRAY_PTR(e->args); + } + rb_block_call(e->obj, e->meth, argc, argv, + enumerator_with_memo_i, memo); + + return memo; +} + +static VALUE next_ii(VALUE i, VALUE obj, int argc, VALUE *argv) { rb_fiber_yield(argc, argv); @@ -454,6 +490,7 @@ rb_define_method(rb_cEnumerator, "each", enumerator_each, 0); rb_define_method(rb_cEnumerator, "each_with_index", enumerator_with_index, 0); rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0); + rb_define_method(rb_cEnumerator, "with_memo", enumerator_with_memo, 1); rb_define_method(rb_cEnumerator, "next", enumerator_next, 0); rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0); Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 16794) +++ test/ruby/test_enumerator.rb (revision 16795) @@ -91,6 +91,25 @@ assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a) end + def test_with_memo + r = 1..10 + assert_equal([55, 3628800], (1..10).each.with_memo([0,1]) {|i, memo| + memo[0] += i + memo[1] *= i + }) + + a = [2,5,2,1,5,3,4,2,1,0] + a.delete_if.with_memo({}) {|i, seen| + if seen.key?(i) + true + else + seen[i] = true + false + end + } + assert_equal([2, 5, 1, 3, 4, 0], a) + end + def test_next_rewind e = @obj.to_enum(:foo, 1, 2, 3) assert_equal(1, e.next) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/