Symmetric difference: Difference between revisions

Content deleted Content added
→‎{{header|Lua}}: Add object-oriented Lua version
→‎{{header|Lua}}: Shortened Lua OO-version
Line 1,247: Line 1,247:


<lang lua>SetPrototype = {
<lang lua>SetPrototype = {
__index = {
union = function(self, other)
local res = Set{}
union = function(self, other)
for k in pairs(self) do res[k] = true end
local res = Set{}
for k in pairs(other) do res[k] = true end
for k in pairs(self) do res[k] = true end
return res
for k in pairs(other) do res[k] = true end
return res
end,
end,
intersection = function(self, other)
local res = Set{}
intersection = function(self, other)
for k in pairs(self) do res[k] = other[k] end
local res = Set{}
return res
for k in pairs(self) do res[k] = other[k] end
return res
end,
end,
difference = function(self, other)
local res = Set{}
difference = function(self, other)
for k in pairs(self) do
local res = Set{}
if not other[k] then res[k] = true end
for k in pairs(self) do
if not other[k] then res[k] = true end
end
return res
end,
symmetric_difference = function(self, other)
return self:difference(other):union(other:difference(self))
end
end
return res
},
end,
symmetric_difference = function(self, other)
return self:difference(other):union(other:difference(self))
end,
-- return string representation of set
-- return string representation of set
tostring = function(self)
__tostring = function(self)
-- list to collect all elements from the set
local l = {}
local l = {}
for e in pairs(self) do l[#l+1] = e end
for k in pairs(self) do l[#l+1] = k end
return "{" .. table.concat(l, ", ") .. "}"
return "{" .. table.concat(l, ", ") .. "}"
end,
end,
-- allow concatenation with other types to yield string
-- print set
["print"] = function(self)
__concat = function(a, b)
print(self:tostring())
return (type(a) == 'string' and a or tostring(a)) ..
(type(b) == 'string' and b or tostring(b))
end
end
}
}
Line 1,282: Line 1,286:
function Set(items)
function Set(items)
local _set = {}
local _set = {}
setmetatable(_set, {__index = SetPrototype})
setmetatable(_set, SetPrototype)
for _, item in ipairs(items) do _set[item] = true end
for _, item in ipairs(items) do _set[item] = true end
return _set
return _set
end
end


A = Set{"John", "Bob", "Mary", "Serena"}
A = Set{"John", "Serena", "Bob", "Mary", "Serena"}
B = Set{"Jim", "Mary", "John", "Bob"}
B = Set{"Jim", "Mary", "John", "Jim", "Bob"}


print "Set A:"
print("Set A: " .. A)
A:print()
print("Set B: " .. B)


print("\nSymm. difference (A\\B)∪(B\\A): " .. A:symmetric_difference(B))
print "\nSet B:"
print("Union A∪B : " .. A:union(B))
A:print()
print("Intersection A∩B : " .. A:intersection(B))

print "\nSymmetric difference (A \\ B) (B \\ A):"
print("Difference A\\B : " .. A:difference(B))
print("Difference B\\A : " .. B:difference(A))</lang>
A:symmetric_difference(B):print()

print "\nUnion A ∪ B:"
A:union(B):print()

print "\nIntersection A ∩ B:"
A:intersection(B):print()

print "\nDifference A \\ B:"
A:difference(B):print()

print "\nDifference B \\ A:"
B:difference(A):print()
</lang>


'''Output:'''
'''Output:'''


Set A:
Set A: {Serena, Mary, John, Bob}
{Mary, Serena, John, Bob}
Set B: {Mary, Jim, John, Bob}
Set B:
{Mary, Jim, John, Bob}
Symmetric difference (A \ B) (B \ A):
{Serena, Jim}
Union A ∪ B:
{John, Mary, Jim, Serena, Bob}
Intersection A ∩ B:
{Mary, John, Bob}
Difference A \ B:
{Serena}
Symm. difference (A\B)∪(B\A): {Serena, Jim}
Difference B \ A:
Union A∪B : {John, Serena, Jim, Mary, Bob}
{Jim}
Intersection A∩B : {Mary, John, Bob}
Difference A\B : {Serena}
Difference B\A : {Jim}


=={{header|Mathematica}}==
=={{header|Mathematica}}==