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

ruby-changes:28767

From: knu <ko1@a...>
Date: Sun, 19 May 2013 02:49:09 +0900 (JST)
Subject: [ruby-changes:28767] knu:r40819 (trunk): * lib/set.rb (Set#delete_if, Set#keep_if): Make Set#delete_if and

knu	2013-05-19 02:48:18 +0900 (Sun, 19 May 2013)

  New Revision: 40819

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40819

  Log:
    * lib/set.rb (Set#delete_if, Set#keep_if): Make Set#delete_if and
      Set#keep_if more space and time efficient by avoiding to_a.

  Modified files:
    trunk/ChangeLog
    trunk/lib/set.rb
    trunk/test/test_set.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40818)
+++ ChangeLog	(revision 40819)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun May 19 02:46:32 2013  Akinori MUSHA  <knu@i...>
+
+	* lib/set.rb (Set#delete_if, Set#keep_if): Make Set#delete_if and
+	  Set#keep_if more space and time efficient by avoiding to_a.
+
 Sun May 19 02:33:09 2013  Akinori MUSHA  <knu@i...>
 
 	* misc/ruby-electric.el (ruby-electric-setup-keymap): Make
Index: lib/set.rb
===================================================================
--- lib/set.rb	(revision 40818)
+++ lib/set.rb	(revision 40819)
@@ -272,7 +272,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L272
   # true, and returns self.
   def delete_if
     block_given? or return enum_for(__method__)
-    to_a.each { |o| @hash.delete(o) if yield(o) }
+    select(&proc).each(&@hash.method(:delete))
     self
   end
 
@@ -280,7 +280,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L280
   # false, and returns self.
   def keep_if
     block_given? or return enum_for(__method__)
-    to_a.each { |o| @hash.delete(o) unless yield(o) }
+    reject(&proc).each(&@hash.method(:delete))
     self
   end
 
Index: test/test_set.rb
===================================================================
--- test/test_set.rb	(revision 40818)
+++ test/test_set.rb	(revision 40819)
@@ -368,6 +368,18 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L368
     assert_equal(Set[1,2,4,5,7,8,10], set)
   end
 
+  def test_keep_if
+    set = Set.new(1..10)
+    ret = set.keep_if { |i| i <= 10 }
+    assert_same(set, ret)
+    assert_equal(Set.new(1..10), set)
+
+    set = Set.new(1..10)
+    ret = set.keep_if { |i| i % 3 != 0 }
+    assert_same(set, ret)
+    assert_equal(Set[1,2,4,5,7,8,10], set)
+  end
+
   def test_collect!
     set = Set[1,2,3,'a','b','c',-1..1,2..4]
 

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

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