[前][次][番号順一覧][スレッド一覧]

ruby-changes:3969

From: ko1@a...
Date: Wed, 13 Feb 2008 21:51:55 +0900 (JST)
Subject: [ruby-changes:3969] mame - Ruby:r15459 (trunk): * proc.c (proc_curry): new method.

mame	2008-02-13 21:51:31 +0900 (Wed, 13 Feb 2008)

  New Revision: 15459

  Modified files:
    trunk/ChangeLog
    trunk/proc.c
    trunk/test/ruby/test_proc.rb

  Log:
    * proc.c (proc_curry): new method. [ruby-dev:33676]
    
    * test/ruby/test_proc.rb: add tests for above.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_proc.rb?r1=15459&r2=15458&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15459&r2=15458&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/proc.c?r1=15459&r2=15458&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15458)
+++ ChangeLog	(revision 15459)
@@ -1,3 +1,9 @@
+Wed Feb 13 21:50:32 2008  Yusuke Endoh  <mame@t...>
+
+	* 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@f...>
 
 	* include/ruby/ruby.h (RObject): add iv_index_tbl for shortcut of
@@ -237,8 +243,8 @@
 
 Sat Feb  9 21:20:28 2008  Yusuke Endoh  <mame@t...>
 
-	* 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@f...>
 
Index: proc.c
===================================================================
--- proc.c	(revision 15458)
+++ proc.c	(revision 15459)
@@ -1608,6 +1608,106 @@
     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 @@
     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);
Index: test/ruby/test_proc.rb
===================================================================
--- test/ruby/test_proc.rb	(revision 15458)
+++ test/ruby/test_proc.rb	(revision 15459)
@@ -137,4 +137,67 @@
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]