зеркало из https://github.com/github/ruby.git
* lib/set.rb (Set#collect, Set#select): Override Enumerable
methods and make them return a set. [ruby-core:17055] (Set#delete_if, Set#collect!, Set#reject!, Set#classify) (Set#divide, Set#delete_if): Return an enumerator if no block is given. (Set#classify): Define an alias `group_by' to override that of Enumerable. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16771 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
a5c9928320
Коммит
ea40df71fc
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
|||
Tue Jun 3 13:41:08 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* lib/set.rb (Set#collect, Set#select): Override Enumerable
|
||||
methods and make them return a set. [ruby-core:17055]
|
||||
(Set#delete_if, Set#collect!, Set#reject!, Set#classify)
|
||||
(Set#divide, Set#delete_if): Return an enumerator if no block is
|
||||
given.
|
||||
(Set#classify): Define an alias `group_by' to override that of
|
||||
Enumerable.
|
||||
|
||||
Tue Jun 3 13:35:40 2008 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||
|
||||
* process.c (run_exec_pgroup): C99 ism.
|
||||
|
|
32
lib/set.rb
32
lib/set.rb
|
@ -250,21 +250,41 @@ class Set
|
|||
# Deletes every element of the set for which block evaluates to
|
||||
# true, and returns self.
|
||||
def delete_if
|
||||
block_given? or return enum_for(__method__)
|
||||
@hash.delete_if { |o,| yield(o) }
|
||||
self
|
||||
end
|
||||
|
||||
# Do collect() destructively.
|
||||
def collect!
|
||||
# Calls the given block once for each element and returns a new set
|
||||
# containing the values returned by the block.
|
||||
def collect
|
||||
block_given? or return enum_for(__method__)
|
||||
set = self.class.new
|
||||
each { |o| set << yield(o) }
|
||||
replace(set)
|
||||
end
|
||||
alias map collect
|
||||
|
||||
# Replaces the values with ones returned by collect().
|
||||
def collect!
|
||||
block_given? or return enum_for(__method__)
|
||||
replace(collect)
|
||||
end
|
||||
alias map! collect!
|
||||
|
||||
# Calls the given block once for each element and returns a new set
|
||||
# containing those elements for which the block returns a true
|
||||
# value.
|
||||
def select
|
||||
block_given? or return enum_for(__method__)
|
||||
set = self.class.new
|
||||
each { |o| set << o if yield(o) }
|
||||
set
|
||||
end
|
||||
|
||||
# Equivalent to Set#delete_if, but returns nil if no changes were
|
||||
# made.
|
||||
def reject!
|
||||
block_given? or return enum_for(__method__)
|
||||
n = size
|
||||
delete_if { |o| yield(o) }
|
||||
size == n ? nil : self
|
||||
|
@ -356,6 +376,8 @@ class Set
|
|||
# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
|
||||
# # 2002=>#<Set: {"f.rb"}>}
|
||||
def classify # :yields: o
|
||||
block_given? or return enum_for(__method__)
|
||||
|
||||
h = {}
|
||||
|
||||
each { |i|
|
||||
|
@ -365,6 +387,7 @@ class Set
|
|||
|
||||
h
|
||||
end
|
||||
alias group_by classify
|
||||
|
||||
# Divides the set into a set of subsets according to the commonality
|
||||
# defined by the given block.
|
||||
|
@ -383,6 +406,8 @@ class Set
|
|||
# # #<Set: {3, 4}>,
|
||||
# # #<Set: {6}>}>
|
||||
def divide(&func)
|
||||
func or return enum_for(__method__)
|
||||
|
||||
if func.arity == 2
|
||||
require 'tsort'
|
||||
|
||||
|
@ -501,6 +526,7 @@ class SortedSet < Set
|
|||
end
|
||||
|
||||
def delete_if
|
||||
block_given? or return enum_for(__method__)
|
||||
n = @hash.size
|
||||
@hash.delete_if { |o,| yield(o) }
|
||||
@keys = nil if @hash.size != n
|
||||
|
|
Загрузка…
Ссылка в новой задаче