[#33948] Schedule for the 1.8.7 release — "Akinori MUSHA" <knu@...>
Hi, developers,
[#33955] --encoding affects script encoding — sheepman <sheepman@...>
こんばんは sheepman です。
なかだです。
[#33962] Ruby1.9.0でのインタプリタ組み込みについての質問 — Masayuki Yamaguchi <Yamaguchi.Masayuki@...>
山口と申します。
[#33966] Re: [ruby-cvs:22881] Ruby:r15644 (trunk): * test/ruby/test_m17n_comb.rb (TestM17NComb::test_str_chomp): test — Tanaka Akira <akr@...>
In article <200802291457.m1TEv6nh008515@ci.ruby-lang.org>,
まつもと ゆきひろです
[#33974] Test::Unit::Collector::Dirがtest_*.rb以外集めてくれない — "Ken Date" <itacchi@...>
こんにちは、伊達です。
[#33983] Re: [ruby-cvs:22913] Re: Ruby:r15674 (trunk): * gc.c (add_heap): sort heaps array in ascending order to use — Yukihiro Matsumoto <matz@...>
まつもと ゆきひろです
In article <E1JWAV5-0001MG-9W@x61.netlab.jp>,
[#34011] Should --verbose be equal to -v ? — Yugui <yugui@...>
Yuguiです。
まつもと ゆきひろです
西山和広です。
Yuguiです。
[#34020] MurmurHash problem — Nobuyoshi Nakada <nobu@...>
なかだです。
[#34030] uint32_t — KIMURA Koichi <kimura.koichi@...>
木村です。
[#34037] Ruby performance gains on SPARC — Yukihiro Matsumoto <matz@...>
まつもと ゆきひろです
[#34067] Array#take,take_while,drop,drop_whlie — "Yusuke ENDOH" <mame@...>
遠藤と申します。
[#34068] lgamma_r requires _REENTRANT on Solaris — "Yusuke ENDOH" <mame@...>
遠藤と申します。
[#34077] 異なるエンコーディングだと同じバイト列でも==にならない件 — rubikitch@...
るびきちです。
[#34086] extend spawn to change attributes of child process. — Tanaka Akira <akr@...>
spaen, system, exec, IO.popen で、起動する子プロセスの属性を
[#34093] 拡張ライブラリ初期化中でのmodule_eval — Kouhei Sutou <kou@...>
須藤です。
[#34095] (再送) Cygwin で Resolv.getaddress が失敗する — Kouhei Yanagita <yanagi@...>
こんにちは。柳田です。
こんばんは、植田と申します。
柳田です。
[#34105] rational.rb, complex.rb and mathn.rb — Tadayoshi Funaba <tadf@...>
rational と complex が組み込みになったことで、lib/mathn.rb の意義は薄
現時点で rational.rb と complex.rb を残しているのは、それが無難だから
で、かなり選択肢を絞った叩き台です。
けいじゅ@いしつかです.
原です。
> 私も Complex の組み込みは Rational とは比較にならないくらい、仕様が決め
まつもと ゆきひろです
> Mathモジュールは伝統的にlibmのラッパーであったので、それを逸
原です。
> (1) (-8)**Rational(1,2) は複素数1.0+1.7320508*i
[#34109] LP64: date.rb:321:in `convert': integer 86400000000000 too big to convert to `int' (RangeError) — Tanaka Akira <akr@...>
LP64 なマシンで test-all が動かなくなっています。
[#34144] [質問2点] C からの定数参照 & thread switching コストの低減 — Hidetoshi NAGAI <nagai@...>
永井@知能.九工大です.
[#34158] Complex組み込み — Masahiro TANAKA <masa16.tanaka@...>
Complexが組み込みになるそうですが、これはcomplex.rbを踏襲して、
原です。
> 今までの Complex は、complex.rb にほぼ残して、たとえば Rational 成分
原です。
> そうです。Complex が難しい、という話を書いておくと、
まつもと ゆきひろです
> |僕としては、/ 演算子の振舞いについて前向きに検討してほしいです。
まつもと ゆきひろです
> ふむ。では、/ のふるまいを
まつもと ゆきひろです
> |僕は、quo がいいと思います。
まつもと ゆきひろです
> となるようですが、別の実装として、
田中です。
> 最初に言っておきますが、気を悪くされたのならすみません。
村田です.
[#34159] ruby-trunk Marshal.dump bug — nagachika <rucila@...>
nagachika と申します。
[#34163] Array#shift/unshift の高速化 — wanabe <s.wanabe@...>
ワナベと申します。
[#34189] Re: [ruby-cvs:23106] Re: Ruby:r15866 (trunk): * numeric.c (num_quo): should convert its operand to Rational. — Tadayoshi Funaba <tadf@...>
間違って送ったので、再送。
> > > Log:
[ruby-dev:34067] Array#take,take_while,drop,drop_whlie
遠藤と申します。
ary.drop(1) は each を経由するので常に O(n) かかり、ちょっと遅いです。
$ time ./ruby -e 'a=[0]*10000; a = a.drop(1) until a.empty?'
real 0m4.440s
user 0m4.310s
sys 0m0.090s
そこで、Array 特化版の Array#take 、take_while 、drop、drop_while を
作ってみました。バッファの共有がきくので速くなります。
$ time ./ruby -e 'a=[0]*10000; a = a.drop(1) until a.empty?'
real 0m0.210s
user 0m0.020s
sys 0m0.030s
どうでしょうか。
Index: array.c
===================================================================
--- array.c (revision 15784)
+++ array.c (working copy)
@@ -3228,8 +3228,100 @@
return result;
}
+/*
+ * call-seq:
+ * ary.take(n) => array
+ *
+ * Returns first n elements from <i>ary</i>.
+ *
+ * a = [1, 2, 3, 4, 5, 0]
+ * a.take(3) # => [1, 2, 3]
+ *
+ */
+static VALUE
+rb_ary_take(VALUE obj, VALUE n)
+{
+ return rb_ary_subseq(obj, 0, FIX2LONG(n));
+}
+/*
+ * call-seq:
+ * ary.take_while {|arr| block } => array
+ *
+ * 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_while {|i| i < 3 } # => [1, 2]
+ *
+ */
+
+static VALUE
+rb_ary_take_while(VALUE ary)
+{
+ VALUE result;
+ long i;
+
+ RETURN_ENUMERATOR(ary, 0, 0);
+ result = rb_ary_new2(RARRAY_LEN(ary));
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ if (!RTEST(rb_yield(RARRAY_PTR(ary)[i]))) break;
+ rb_ary_push(result, rb_ary_elt(ary, i));
+ }
+ return result;
+}
+
+/*
+ * call-seq:
+ * ary.drop(n) => array
+ *
+ * Drops first n elements from <i>ary</i>, and returns rest elements
+ * in an array.
+ *
+ * a = [1, 2, 3, 4, 5, 0]
+ * a.drop(3) # => [4, 5, 0]
+ *
+ */
+
+static VALUE
+rb_ary_drop(VALUE ary, VALUE n)
+{
+ VALUE result;
+
+ result = rb_ary_subseq(ary, FIX2LONG(n), RARRAY_LEN(ary));
+ if (result == Qnil) result = rb_ary_new();
+ return result;
+}
+
+/*
+ * call-seq:
+ * ary.drop_while {|arr| block } => array
+ *
+ * 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_while {|i| i < 3 } # => [3, 4, 5, 0]
+ *
+ */
+
+static VALUE
+rb_ary_drop_while(VALUE ary)
+{
+ VALUE result;
+ long i;
+
+ RETURN_ENUMERATOR(ary, 0, 0);
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ if (!RTEST(rb_yield(RARRAY_PTR(ary)[i]))) break;
+ }
+ return rb_ary_drop(ary, LONG2FIX(i));
+}
+
+
+
/* Arrays are ordered, integer-indexed collections of any object.
* Array indexing starts at 0, as in C or Java. A negative index is
* assumed to be relative to the end of the array---that is, an index of -1
@@ -3332,5 +3424,10 @@
rb_define_method(rb_cArray, "combination", rb_ary_combination, 1);
rb_define_method(rb_cArray, "product", rb_ary_product, -1);
+ rb_define_method(rb_cArray, "take", rb_ary_take, 1);
+ rb_define_method(rb_cArray, "take_while", rb_ary_take_while, 0);
+ rb_define_method(rb_cArray, "drop", rb_ary_drop, 1);
+ rb_define_method(rb_cArray, "drop_while", rb_ary_drop_while, 0);
+
id_cmp = rb_intern("<=>");
}
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb (revision 15784)
+++ test/ruby/test_array.rb (working copy)
@@ -1229,4 +1229,20 @@
assert_equal(@cls[].permutation(0).to_a, @cls[[]])
end
+
+ def test_take
+ assert_equal([1,2,3], [1,2,3,4,5,0].take(3))
+ end
+
+ def test_take_while
+ assert_equal([1,2], [1,2,3,4,5,0].take_while {|i| i < 3 })
+ end
+
+ def test_drop
+ assert_equal([4,5,0], [1,2,3,4,5,0].drop(3))
+ end
+
+ def test_drop_while
+ assert_equal([3,4,5,0], [1,2,3,4,5,0].drop_while {|i| i < 3 })
+ end
end
--
Yusuke ENDOH <mame@tsg.ne.jp>