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

ruby-changes:40510

From: knu <ko1@a...>
Date: Mon, 16 Nov 2015 16:30:00 +0900 (JST)
Subject: [ruby-changes:40510] knu:r52591 (trunk): * lib/set.rb (Hash#flatten!, #add?, #delete?, #collect!, #reject!,

knu	2015-11-16 16:29:37 +0900 (Mon, 16 Nov 2015)

  New Revision: 52591

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

  Log:
    * lib/set.rb (Hash#flatten!, #add?, #delete?, #collect!, #reject!,
      #select!, #^, #classify): Micro-optimize some methods for
      performance and readability.

  Modified files:
    trunk/ChangeLog
    trunk/lib/set.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52590)
+++ ChangeLog	(revision 52591)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Nov 16 16:28:30 2015  Akinori MUSHA  <knu@i...>
+
+	* lib/set.rb (Hash#flatten!, #add?, #delete?, #collect!, #reject!,
+	  #select!, #^, #classify): Micro-optimize some methods for
+	  performance and readability.
+
 Mon Nov 16 16:17:58 2015  SHIBATA Hiroshi  <hsbt@r...>
 
 	* ChangeLog: fixed accidentally commit.
Index: lib/set.rb
===================================================================
--- lib/set.rb	(revision 52590)
+++ lib/set.rb	(revision 52591)
@@ -200,11 +200,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L200
   # Equivalent to Set#flatten, but replaces the receiver with the
   # result in place.  Returns nil if no modifications were made.
   def flatten!
-    if detect { |e| e.is_a?(Set) }
-      replace(flatten())
-    else
-      nil
-    end
+    replace(flatten()) if any? { |e| e.is_a?(Set) }
   end
 
   # Returns true if the set contains the given object.
@@ -320,11 +316,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L316
   # Adds the given object to the set and returns self.  If the
   # object is already in the set, returns nil.
   def add?(o)
-    if include?(o)
-      nil
-    else
-      add(o)
-    end
+    add(o) unless include?(o)
   end
 
   # Deletes the given object from the set and returns self.  Use +subtract+ to
@@ -337,11 +329,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L329
   # Deletes the given object from the set and returns self.  If the
   # object is not in the set, returns nil.
   def delete?(o)
-    if include?(o)
-      delete(o)
-    else
-      nil
-    end
+    delete(o) if include?(o)
   end
 
   # Deletes every element of the set for which block evaluates to
@@ -367,9 +355,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L355
   # Replaces the elements with ones returned by collect().
   def collect!
     block_given? or return enum_for(__method__)
-    set = self.class.new
-    each { |o| set << yield(o) }
-    replace(set)
+    replace(self.class.new(self) { |o| yield(o) })
   end
   alias map! collect!
 
@@ -379,7 +365,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L365
     block or return enum_for(__method__)
     n = size
     delete_if(&block)
-    size == n ? nil : self
+    self if size != n
   end
 
   # Equivalent to Set#keep_if, but returns nil if no changes were
@@ -388,7 +374,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L374
     block or return enum_for(__method__)
     n = size
     keep_if(&block)
-    size == n ? nil : self
+    self if size != n
   end
 
   # Merges the elements of the given enumerable object to the set and
@@ -439,7 +425,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L425
   # ((set | enum) - (set & enum)).
   def ^(enum)
     n = Set.new(enum)
-    each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
+    each { |o| n.add(o) unless n.delete?(o) }
     n
   end
 
@@ -485,8 +471,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L471
     h = {}
 
     each { |i|
-      x = yield(i)
-      (h[x] ||= self.class.new).add(i)
+      (h[yield(i)] ||= self.class.new).add(i)
     }
 
     h

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

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