Add Comparable#uniq; CRuby2.4

This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-10-17 12:46:51 +09:00
parent 4e365156d1
commit db8265f428
2 changed files with 31 additions and 0 deletions
+16
View File
@@ -708,4 +708,20 @@ module Enumerable
def nil.to_h
{}
end
def uniq(&block)
hash = {}
if block
self.each do|*v|
v = v.__svalue
hash[block.call(v)] ||= v
end
else
self.each do|*v|
v = v.__svalue
hash[v] ||= v
end
end
hash.values
end
end
+15
View File
@@ -158,6 +158,21 @@ class Enumerator
}
end
def uniq(&block)
hash = {}
Lazy.new(self){|yielder, val|
if block
v = block.call(val)
else
v = val
end
unless hash.include?(v)
yielder << val
hash[v] = val
end
}
end
alias force to_a
end
end