mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* proc.c (proc_curry): new method. [ruby-dev:33676]
* test/ruby/test_proc.rb: add tests for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15459 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									8f842d71e9
								
							
						
					
					
						commit
						cd6dcfb05b
					
				
					 3 changed files with 172 additions and 2 deletions
				
			
		
							
								
								
									
										10
									
								
								ChangeLog
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								ChangeLog
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,3 +1,9 @@
 | 
			
		|||
Wed Feb 13 21:50:32 2008  Yusuke Endoh  <mame@tsg.ne.jp>
 | 
			
		||||
 | 
			
		||||
	* proc.c (proc_curry): new method. [ruby-dev:33676]
 | 
			
		||||
 | 
			
		||||
	* test/ruby/test_proc.rb: add tests for above.
 | 
			
		||||
 | 
			
		||||
Wed Feb 13 20:48:50 2008  Tanaka Akira  <akr@fsij.org>
 | 
			
		||||
 | 
			
		||||
	* include/ruby/ruby.h (RObject): add iv_index_tbl for shortcut of
 | 
			
		||||
| 
						 | 
				
			
			@ -237,8 +243,8 @@ Sat Feb  9 23:22:52 2008  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		|||
 | 
			
		||||
Sat Feb  9 21:20:28 2008  Yusuke Endoh  <mame@tsg.ne.jp>
 | 
			
		||||
 | 
			
		||||
	* test/ruby/test_math.rb: add tests for Math#gamma, Math#lgamma and
 | 
			
		||||
	  Math#cbrt, and use assert_in_delta instead of assert.
 | 
			
		||||
	* test/ruby/test_math.rb: add tests for Math.gamma, Math.lgamma and
 | 
			
		||||
	  Math.cbrt, and use assert_in_delta instead of assert.
 | 
			
		||||
 | 
			
		||||
Sat Feb  9 18:34:45 2008  Tanaka Akira  <akr@fsij.org>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										101
									
								
								proc.c
									
										
									
									
									
								
							
							
						
						
									
										101
									
								
								proc.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1608,6 +1608,106 @@ proc_binding(VALUE self)
 | 
			
		|||
    return bindval;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv);
 | 
			
		||||
 | 
			
		||||
static VALUE
 | 
			
		||||
make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
 | 
			
		||||
{
 | 
			
		||||
    VALUE args = rb_ary_new2(3);
 | 
			
		||||
    RARRAY_PTR(args)[0] = proc;
 | 
			
		||||
    RARRAY_PTR(args)[1] = passed;
 | 
			
		||||
    RARRAY_PTR(args)[2] = arity;
 | 
			
		||||
    RARRAY_LEN(args) = 3;
 | 
			
		||||
    rb_ary_freeze(passed);
 | 
			
		||||
    rb_ary_freeze(args);
 | 
			
		||||
    return rb_proc_new(curry, args);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static VALUE
 | 
			
		||||
curry(VALUE dummy, VALUE args, int argc, VALUE *argv)
 | 
			
		||||
{
 | 
			
		||||
    VALUE proc, passed, arity;
 | 
			
		||||
    proc = RARRAY_PTR(args)[0];
 | 
			
		||||
    passed = RARRAY_PTR(args)[1];
 | 
			
		||||
    arity = RARRAY_PTR(args)[2];
 | 
			
		||||
 | 
			
		||||
    passed = rb_ary_plus(passed, rb_ary_new4(argc, argv));
 | 
			
		||||
    rb_ary_freeze(passed);
 | 
			
		||||
    if(RARRAY_LEN(passed) < FIX2INT(arity)) {
 | 
			
		||||
	arity = make_curry_proc(proc, passed, arity);
 | 
			
		||||
	return arity;
 | 
			
		||||
    }
 | 
			
		||||
    arity = rb_proc_call(proc, passed);
 | 
			
		||||
    return arity;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 /*
 | 
			
		||||
  *  call-seq:
 | 
			
		||||
  *     prc.curry         => a_proc
 | 
			
		||||
  *     prc.curry(arity)  => a_proc
 | 
			
		||||
  *
 | 
			
		||||
  *  Returns a curried proc. If the optional <i>arity</i> argument is given,
 | 
			
		||||
  *  it determines the number of arguments.
 | 
			
		||||
  *  A curried proc receives some arguments. If a sufficient number of
 | 
			
		||||
  *  arguments are supplied, it passes the supplied arguments to the original
 | 
			
		||||
  *  proc and returns the result. Otherwise, returns another curried proc that
 | 
			
		||||
  *  takes the rest of arguments.
 | 
			
		||||
  *
 | 
			
		||||
  *     b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
 | 
			
		||||
  *     p b.curry[1][2][3]           #=> 6
 | 
			
		||||
  *     p b.curry[1, 2][3, 4]        #=> 6
 | 
			
		||||
  *     p b.curry(5)[1][2][3][4][5]  #=> 6
 | 
			
		||||
  *     p b.curry(5)[1, 2][3, 4][5]  #=> 6
 | 
			
		||||
  *     p b.curry(1)[1]              #=> 1
 | 
			
		||||
  *
 | 
			
		||||
  *     b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
 | 
			
		||||
  *     p b.curry[1][2][3]           #=> 6
 | 
			
		||||
  *     p b.curry[1, 2][3, 4]        #=> 10
 | 
			
		||||
  *     p b.curry(5)[1][2][3][4][5]  #=> 15
 | 
			
		||||
  *     p b.curry(5)[1, 2][3, 4][5]  #=> 15
 | 
			
		||||
  *     p b.curry(1)[1]              #=> 1
 | 
			
		||||
  *
 | 
			
		||||
  *     b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
 | 
			
		||||
  *     p b.curry[1][2][3]           #=> 6
 | 
			
		||||
  *     p b.curry[1, 2][3, 4]        #=> wrong number of arguments (4 or 3)
 | 
			
		||||
  *     p b.curry(5)                 #=> wrong number of arguments (5 or 3)
 | 
			
		||||
  *     p b.curry(1)                 #=> wrong number of arguments (1 or 3)
 | 
			
		||||
  *
 | 
			
		||||
  *     b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
 | 
			
		||||
  *     p b.curry[1][2][3]           #=> 6
 | 
			
		||||
  *     p b.curry[1, 2][3, 4]        #=> 10
 | 
			
		||||
  *     p b.curry(5)[1][2][3][4][5]  #=> 15
 | 
			
		||||
  *     p b.curry(5)[1, 2][3, 4][5]  #=> 15
 | 
			
		||||
  *     p b.curry(1)                 #=> wrong number of arguments (1 or 3)
 | 
			
		||||
  *
 | 
			
		||||
  *     b = proc { :foo }
 | 
			
		||||
  *     p b.curry[]                  #=> :foo
 | 
			
		||||
  */
 | 
			
		||||
static VALUE
 | 
			
		||||
proc_curry(int argc, VALUE *argv, VALUE self)
 | 
			
		||||
{
 | 
			
		||||
    int sarity, marity = FIX2INT(proc_arity(self));
 | 
			
		||||
    VALUE arity, opt = Qfalse;
 | 
			
		||||
 | 
			
		||||
    if (marity < 0) {
 | 
			
		||||
	marity = -marity - 1;
 | 
			
		||||
	opt = Qtrue;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    rb_scan_args(argc, argv, "01", &arity);
 | 
			
		||||
    if (NIL_P(arity)) {
 | 
			
		||||
	arity = INT2FIX(marity);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
	sarity = FIX2INT(arity);
 | 
			
		||||
	if (proc_lambda_p(self) && (sarity < marity || (sarity > marity && !opt))) {
 | 
			
		||||
	    rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", sarity, marity);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return make_curry_proc(self, rb_ary_new(), arity);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  <code>Proc</code> objects are blocks of code that have been bound to
 | 
			
		||||
 *  a set of local variables. Once bound, the code may be called in
 | 
			
		||||
| 
						 | 
				
			
			@ -1646,6 +1746,7 @@ Init_Proc(void)
 | 
			
		|||
    rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
 | 
			
		||||
    rb_define_method(rb_cProc, "lambda?", proc_lambda_p, 0);
 | 
			
		||||
    rb_define_method(rb_cProc, "binding", proc_binding, 0);
 | 
			
		||||
    rb_define_method(rb_cProc, "curry", proc_curry, -1);
 | 
			
		||||
 | 
			
		||||
    /* Exceptions */
 | 
			
		||||
    rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -137,4 +137,67 @@ class TestProc < Test::Unit::TestCase
 | 
			
		|||
    assert_equal "OK", b.call
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_curry
 | 
			
		||||
    b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
 | 
			
		||||
    assert_equal(6, b.curry[1][2][3])
 | 
			
		||||
    assert_equal(6, b.curry[1, 2][3, 4])
 | 
			
		||||
    assert_equal(6, b.curry(5)[1][2][3][4][5])
 | 
			
		||||
    assert_equal(6, b.curry(5)[1, 2][3, 4][5])
 | 
			
		||||
    assert_equal(1, b.curry(1)[1])
 | 
			
		||||
 | 
			
		||||
    b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
 | 
			
		||||
    assert_equal(6, b.curry[1][2][3])
 | 
			
		||||
    assert_equal(10, b.curry[1, 2][3, 4])
 | 
			
		||||
    assert_equal(15, b.curry(5)[1][2][3][4][5])
 | 
			
		||||
    assert_equal(15, b.curry(5)[1, 2][3, 4][5])
 | 
			
		||||
    assert_equal(1, b.curry(1)[1])
 | 
			
		||||
 | 
			
		||||
    b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
 | 
			
		||||
    assert_equal(6, b.curry[1][2][3])
 | 
			
		||||
    assert_raise(ArgumentError) { b.curry[1, 2][3, 4] }
 | 
			
		||||
    assert_raise(ArgumentError) { b.curry(5) }
 | 
			
		||||
    assert_raise(ArgumentError) { b.curry(1) }
 | 
			
		||||
 | 
			
		||||
    b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
 | 
			
		||||
    assert_equal(6, b.curry[1][2][3])
 | 
			
		||||
    assert_equal(10, b.curry[1, 2][3, 4])
 | 
			
		||||
    assert_equal(15, b.curry(5)[1][2][3][4][5])
 | 
			
		||||
    assert_equal(15, b.curry(5)[1, 2][3, 4][5])
 | 
			
		||||
    assert_raise(ArgumentError) { b.curry(1) }
 | 
			
		||||
 | 
			
		||||
    b = proc { :foo }
 | 
			
		||||
    assert_equal(:foo, b.curry[])
 | 
			
		||||
  end
 | 
			
		||||
 
 | 
			
		||||
  def test_curry_ski_fib
 | 
			
		||||
    s = proc {|f, g, x| f[x][g[x]] }.curry
 | 
			
		||||
    k = proc {|x, y| x }.curry
 | 
			
		||||
    i = proc {|x| x }.curry
 | 
			
		||||
 | 
			
		||||
    fib = []
 | 
			
		||||
    inc = proc {|x| fib[-1] += 1; x }.curry
 | 
			
		||||
    ret = proc {|x| throw :end if fib.size > 10; fib << 0; x }.curry
 | 
			
		||||
 | 
			
		||||
    catch(:end) do
 | 
			
		||||
      s[
 | 
			
		||||
        s[s[i][i]][k[i]]
 | 
			
		||||
      ][
 | 
			
		||||
        k[inc]
 | 
			
		||||
      ][
 | 
			
		||||
        s[
 | 
			
		||||
          s[
 | 
			
		||||
            k[s]
 | 
			
		||||
          ][
 | 
			
		||||
            s[k[s[k[s]]]
 | 
			
		||||
          ][
 | 
			
		||||
            s[s[k[s]][s[k[s[k[ret]]]][s[k[s[i]]][k]]]][k]]
 | 
			
		||||
          ]
 | 
			
		||||
        ][
 | 
			
		||||
          k[s[k[s]][k]]
 | 
			
		||||
        ]
 | 
			
		||||
      ]
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    assert_equal(fib, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89])
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue