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{} |
|||
for k in pairs( |
for k in pairs(self) do res[k] = true end |
||
for k in pairs(other) do res[k] = true end |
|||
return res |
|||
end, |
end, |
||
intersection = function(self, other) |
|||
intersection = function(self, other) |
|||
local res = Set{} |
|||
for k in pairs(self) do res[k] = other[k] end |
|||
return res |
|||
end, |
end, |
||
difference = function(self, other) |
|||
difference = function(self, other) |
|||
local res = Set{} |
|||
for k in pairs(self) do |
|||
if not other[k] then res[k] = true end |
|||
⚫ | |||
return res |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
}, |
|||
⚫ | |||
symmetric_difference = function(self, other) |
|||
⚫ | |||
⚫ | |||
-- return string representation of set |
-- return string representation of set |
||
__tostring = function(self) |
|||
-- list to collect all elements from the set |
|||
local l = {} |
local l = {} |
||
for |
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 |
|||
__concat = function(a, b) |
|||
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, |
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 |
print("Set A: " .. A) |
||
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 |
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, |
Set B: {Mary, Jim, John, Bob} |
||
Set B: |
|||
{Mary, Jim, John, Bob} |
|||
⚫ | |||
{Serena, Jim} |
|||
Union A ∪ B: |
|||
{John, Mary, Jim, Serena, Bob} |
|||
Intersection A ∩ B: |
|||
{Mary, John, Bob} |
|||
⚫ | |||
{Serena} |
|||
⚫ | |||
⚫ | |||
Union A∪B : {John, Serena, Jim, Mary, Bob} |
|||
{Jim} |
|||
Intersection A∩B : {Mary, John, Bob} |
|||
⚫ | |||
⚫ | |||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |