mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #1044 from kouki-o-iij/pr-array-ext
add method(uniq, -, |, &, flatten, compact) of Array to mruby-array-ext
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
class Array
|
||||
def uniq!
|
||||
ary = self.dup
|
||||
result = []
|
||||
while ary.size > 0
|
||||
result << ary.shift
|
||||
ary.delete(result.last)
|
||||
end
|
||||
self.replace(result)
|
||||
end
|
||||
|
||||
def uniq
|
||||
self.dup.uniq!
|
||||
end
|
||||
|
||||
def -(elem)
|
||||
raise TypeError, "can't convert to Array" unless elem.class == Array
|
||||
|
||||
hash = {}
|
||||
array = []
|
||||
elem.each { |x| hash[x] = true }
|
||||
self.each { |x| array << x unless hash[x] }
|
||||
array
|
||||
end
|
||||
|
||||
def |(elem)
|
||||
raise TypeError, "can't convert to Array" unless elem.class == Array
|
||||
|
||||
(self + elem).uniq!
|
||||
end
|
||||
|
||||
def &(elem)
|
||||
raise TypeError, "can't convert to Array" unless elem.class == Array
|
||||
|
||||
hash = {}
|
||||
array = []
|
||||
elem.each{|v| hash[v] = true }
|
||||
self.each do |v|
|
||||
if hash[v]
|
||||
array << v
|
||||
hash.delete v
|
||||
end
|
||||
end
|
||||
array
|
||||
end
|
||||
|
||||
def flatten(depth=nil)
|
||||
ar = []
|
||||
self.each do |e|
|
||||
if e.is_a?(Array) && (depth.nil? || depth > 0)
|
||||
ar += e.flatten(depth.nil? ? nil : depth - 1)
|
||||
else
|
||||
ar << e
|
||||
end
|
||||
end
|
||||
ar
|
||||
end
|
||||
|
||||
def flatten!
|
||||
self.replace(self.flatten)
|
||||
end
|
||||
|
||||
def compact
|
||||
result = self.dup
|
||||
result.compact!
|
||||
result
|
||||
end
|
||||
|
||||
def compact!
|
||||
result = self.select { |e| e != nil }
|
||||
self.replace(result)
|
||||
end
|
||||
end
|
||||
@@ -28,3 +28,82 @@ assert("Array#rassoc") do
|
||||
a.rassoc("four").nil?
|
||||
end
|
||||
|
||||
assert("Array#uniq!") do
|
||||
a = [1, 2, 3, 1]
|
||||
a.uniq!
|
||||
a == [1, 2, 3]
|
||||
end
|
||||
|
||||
assert("Array#uniq") do
|
||||
a = [1, 2, 3, 1]
|
||||
a.uniq == [1, 2, 3] && a == [1, 2, 3, 1]
|
||||
end
|
||||
|
||||
assert("Array#-") do
|
||||
a = [1, 2, 3, 1]
|
||||
b = [1]
|
||||
c = 1
|
||||
e1 = nil
|
||||
|
||||
begin
|
||||
a - c
|
||||
rescue => e1
|
||||
end
|
||||
|
||||
(a - b) == [2, 3] and e1.class == TypeError and a == [1, 2, 3, 1]
|
||||
end
|
||||
|
||||
assert("Array#|") do
|
||||
a = [1, 2, 3, 1]
|
||||
b = [1, 4]
|
||||
c = 1
|
||||
e1 = nil
|
||||
|
||||
begin
|
||||
a | c
|
||||
rescue => e1
|
||||
end
|
||||
|
||||
(a | b) == [1, 2, 3, 4] and e1.class == TypeError and a == [1, 2, 3, 1]
|
||||
end
|
||||
|
||||
assert("Array#&") do
|
||||
a = [1, 2, 3, 1]
|
||||
b = [1, 4]
|
||||
c = 1
|
||||
e1 = nil
|
||||
|
||||
begin
|
||||
a & c
|
||||
rescue => e1
|
||||
end
|
||||
|
||||
(a & b) == [1] and e1.class == TypeError and a == [1, 2, 3, 1]
|
||||
end
|
||||
|
||||
assert("Array#flatten") do
|
||||
[1, 2, "3", {4=>5}, :'6'] == [1, 2, "3", {4=>5}, :'6'].flatten and
|
||||
[1, 2, 3, 4, 5, 6] == [1, 2, [3, 4, 5], 6].flatten and
|
||||
[1, 2, 3, 4, 5, 6] == [1, 2, [3, [4, 5], 6]].flatten and
|
||||
[1, [2, [3, [4, [5, [6]]]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(0) and
|
||||
[1, 2, [3, [4, [5, [6]]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(1) and
|
||||
[1, 2, 3, [4, [5, [6]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(2) and
|
||||
[1, 2, 3, 4, [5, [6]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(3) and
|
||||
[1, 2, 3, 4, 5, [6]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(4) and
|
||||
[1, 2, 3, 4, 5, 6] == [1, [2, [3, [4, [5, [6]]]]]].flatten(5)
|
||||
end
|
||||
|
||||
assert("Array#flatten!") do
|
||||
[1, 2, 3, 4, 5, 6] == [1, 2, [3, [4, 5], 6]].flatten!
|
||||
end
|
||||
|
||||
assert("Array#compact") do
|
||||
a = [1, nil, "2", nil, :t, false, nil]
|
||||
a.compact == [1, "2", :t, false] && a == [1, nil, "2", nil, :t, false, nil]
|
||||
end
|
||||
|
||||
assert("Array#compact!") do
|
||||
a = [1, nil, "2", nil, :t, false, nil]
|
||||
a.compact!
|
||||
a == [1, "2", :t, false]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user