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

ruby-changes:2303

From: ko1@a...
Date: 31 Oct 2007 08:43:46 +0900
Subject: [ruby-changes:2303] matz - Ruby:r13794 (trunk): * enum.c (enum_take_while): separate with-block form.

matz	2007-10-31 08:43:26 +0900 (Wed, 31 Oct 2007)

  New Revision: 13794

  Modified files:
    trunk/ChangeLog
    trunk/enum.c
    trunk/version.h

  Log:
    * enum.c (enum_take_while): separate with-block form.
    
    * enum.c (drop_while_i): ditto.
    
    * enum.c (enum_butfirst): abandon butfirst method.  reverted.

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/version.h?r1=13794&r2=13793
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13794&r2=13793
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/enum.c?r1=13794&r2=13793

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 13793)
+++ ChangeLog	(revision 13794)
@@ -1,3 +1,11 @@
+Wed Oct 31 03:13:41 2007  Yukihiro Matsumoto  <matz@r...>
+
+	* enum.c (enum_take_while): separate with-block form.
+
+	* enum.c (drop_while_i): ditto.
+
+	* enum.c (enum_butfirst): abandon butfirst method.  reverted.
+
 Tue Oct 30 10:03:43 2007  Yukihiro Matsumoto  <matz@r...>
 
 	* enum.c (enum_butfirst): add a new method to iterates over
Index: enum.c
===================================================================
--- enum.c	(revision 13793)
+++ enum.c	(revision 13794)
@@ -567,6 +567,7 @@
     return ary[1];
 }
 
+
 /*
  *  call-seq:
  *     enum.sort                     => array
@@ -1330,44 +1331,8 @@
     return obj;
 }
 
-static VALUE
-butfirst_i(VALUE val, long *n)
-{
-    
-    if (*n > 0) {
-	(*n)--;
-	return Qnil;
-    }
-    else {
-	return rb_yield(val);
-    }
-}
 
-/*
- *  call-seq:
- *    e.butfirst {|x| ... }
- *    e.butfirst(n) {|x| ... }
- *
- *  Iterates the given block for each elements except for first n elements.
- *  <i>n</i> defaults to 1.
- *
- */
 static VALUE
-enum_butfirst(int argc, VALUE *argv, VALUE obj)
-{
-    VALUE tmp;
-    long n;
-
-    rb_scan_args(argc, argv, "01", &tmp);
-    RETURN_ENUMERATOR(obj, argc, argv);
-    if (argc == 0) n = 1;
-    else n = NUM2LONG(tmp);
-
-    rb_block_call(obj, id_each, 0, 0, butfirst_i, (VALUE)&n);
-    return obj;
-}
-
-static VALUE
 zip_i(VALUE val, NODE *memo)
 {
     volatile VALUE result = memo->u1.value;
@@ -1444,49 +1409,57 @@
     return Qnil;
 }
 
+/*
+ *  call-seq:
+ *     enum.take(n)               => array
+ *  
+ *  Returns first n elements from <i>enum</i>.
+ *     
+ *     a = [1, 2, 3, 4, 5, 0]
+ *     a.take(3)             # => [1, 2, 3]
+ *     
+ */
+
 static VALUE
-take_iter_i(VALUE i, VALUE *arg)
+enum_take(VALUE obj, VALUE n)
 {
+    VALUE args[2];
+
+    args[1] = NUM2LONG(n);
+    args[0] = rb_ary_new2(args[1]);
+    rb_block_call(obj, id_each, 0, 0, take_i, (VALUE)args);
+    return args[0];
+}
+
+
+static VALUE
+take_while_i(VALUE i, VALUE *ary)
+{
     if (!rb_yield(i)) rb_iter_break();
-    rb_ary_push(arg[0], i);
+    rb_ary_push(*ary, i);
     return Qnil;
 }
 
 /*
  *  call-seq:
- *     enum.take(n)               => array
- *     enum.take {|arr| block }   => array
+ *     enum.take_while {|arr| block }   => array
  *  
- *  Without a block, returns first n elements from <i>enum</i>.
- *  With a block, passes elements to the block until the block
- *  returns nil or false, then stops iterating and returns an
- *  array of all prior elements.
+ *  Passes elements to the block until the block returns nil or false,
+ *  then stops iterating and returns an array of all prior elements.
  *     
  *     a = [1, 2, 3, 4, 5, 0]
- *     
- *     a.take(3)             # => [1, 2, 3]
  *     a.take {|i| i < 3 }   # => [1, 2]
  *     
  */
 
 static VALUE
-enum_take(int argc, VALUE *argv, VALUE obj)
+enum_take_while(VALUE obj)
 {
-    VALUE args[2];
+    VALUE ary;
 
-    if (!rb_block_given_p()) {
-	VALUE vlen;
-
-	rb_scan_args(argc, argv, "1", &vlen);
-	args[1] = NUM2LONG(vlen);
-	args[0] = rb_ary_new2(args[1]);
-	rb_block_call(obj, id_each, 0, 0, take_i, (VALUE)args);
-	return args[0];
-    }
-    rb_scan_args(argc, argv, "0");
-    args[0] = rb_ary_new();
-    rb_block_call(obj, id_each, 0, 0, take_iter_i, (VALUE)args);
-    return args[0];
+    ary = rb_ary_new();
+    rb_block_call(obj, id_each, 0, 0, take_while_i, (VALUE)&ary);
+    return ary;
 }
 
 static VALUE
@@ -1501,53 +1474,63 @@
     return Qnil;
 }
 
+/*
+ *  call-seq:
+ *     enum.drop(n)               => array
+ *  
+ *  Drops first n elements from <i>enum</i>, and returns rest elements
+ *  in an array.
+ *     
+ *     a = [1, 2, 3, 4, 5, 0]
+ *     a.drop(3)             # => [4, 5, 0]
+ *     
+ */
+
 static VALUE
-drop_iter_i(VALUE i, VALUE *arg)
+enum_drop(VALUE obj, VALUE n)
 {
-    if (!arg[1] && !rb_yield(i)) {
-	arg[1] = Qtrue;
+    VALUE args[2];
+
+    args[0] = rb_ary_new2(args[1]);
+    args[1] = NUM2ULONG(n);
+    rb_block_call(obj, id_each, 0, 0, drop_i, (VALUE)args);
+    return args[0];
+}
+
+
+static VALUE
+drop_while_i(VALUE i, VALUE *args)
+{
+    if (!args[1] && !rb_yield(i)) {
+	args[1] = Qtrue;
     }
-    if (arg[1]) {
-	rb_ary_push(arg[0], i);
+    if (args[1]) {
+	rb_ary_push(args[0], i);
     }
     return Qnil;
 }
 
 /*
  *  call-seq:
- *     enum.drop(n)               => array
- *     enum.drop {|arr| block }   => array
+ *     enum.drop_while {|arr| block }   => array
  *  
- *  Without a block, drops first n elements from <i>enum</i>, and returns
- *  rest elements in an array.  With a block, drops elements up to, but
- *  not including, the first element for which the block returns nil or false
- *  and returns an array containing the remaining elements.
+ *  Drops elements up to, but not including, the first element for
+ *  which the block returns nil or false and returns an array
+ *  containing the remaining elements.
  *     
  *     a = [1, 2, 3, 4, 5, 0]
- *     
- *     a.drop(3)             # => [4, 5, 0]
  *     a.drop {|i| i < 3 }   # => [3, 4, 5, 0]
  *     
  */
 
 static VALUE
-enum_drop(int argc, VALUE *argv, VALUE obj)
+enum_drop_while(VALUE obj)
 {
     VALUE args[2];
 
-    if (!rb_block_given_p()) {
-	VALUE vlen;
-
-	rb_scan_args(argc, argv, "1", &vlen);
-	args[1] = NUM2LONG(vlen);
-	args[0] = rb_ary_new2(args[1]);
-	rb_block_call(obj, id_each, 0, 0, drop_i, (VALUE)args);
-	return args[0];
-    }
-    rb_scan_args(argc, argv, "0");
     args[0] = rb_ary_new();
     args[1] = Qfalse;
-    rb_block_call(obj, id_each, 0, 0, drop_iter_i, (VALUE)args);
+    rb_block_call(obj, id_each, 0, 0, drop_while_i, (VALUE)args);
     return args[0];
 }
 
@@ -1643,10 +1626,11 @@
     rb_define_method(rb_mEnumerable,"member?", enum_member, 1);
     rb_define_method(rb_mEnumerable,"include?", enum_member, 1);
     rb_define_method(rb_mEnumerable,"each_with_index", enum_each_with_index, -1);
-    rb_define_method(rb_mEnumerable, "butfirst", enum_butfirst, -1);
     rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
-    rb_define_method(rb_mEnumerable, "take", enum_take, -1);
-    rb_define_method(rb_mEnumerable, "drop", enum_drop, -1);
+    rb_define_method(rb_mEnumerable, "take", enum_take, 1);
+    rb_define_method(rb_mEnumerable, "take_while", enum_take_while, 0);
+    rb_define_method(rb_mEnumerable, "drop", enum_drop, 1);
+    rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
     rb_define_method(rb_mEnumerable, "cycle", enum_cycle, 0);
 
     id_eqq  = rb_intern("===");
Index: version.h
===================================================================
--- version.h	(revision 13793)
+++ version.h	(revision 13794)
@@ -1,7 +1,7 @@
 #define RUBY_VERSION "1.9.0"
-#define RUBY_RELEASE_DATE "2007-10-30"
+#define RUBY_RELEASE_DATE "2007-10-31"
 #define RUBY_VERSION_CODE 190
-#define RUBY_RELEASE_CODE 20071030
+#define RUBY_RELEASE_CODE 20071031
 #define RUBY_PATCHLEVEL 0
 
 #define RUBY_VERSION_MAJOR 1
@@ -9,7 +9,7 @@
 #define RUBY_VERSION_TEENY 0
 #define RUBY_RELEASE_YEAR 2007
 #define RUBY_RELEASE_MONTH 10
-#define RUBY_RELEASE_DAY 30
+#define RUBY_RELEASE_DAY 31
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];

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

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