Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -4,5 +4,5 @@ a = fromList ["John", "Bob", "Mary", "Serena"]
b = fromList ["Jim", "Mary", "John", "Bob"]
(-|-) :: Ord a => Set a -> Set a -> Set a
(-|-) x y = (x \\ y) `union` (y \\ x)
x -|- y = (x \\ y) `union` (y \\ x)
-- Equivalently: (x `union` y) \\ (x `intersect` y)

View file

@ -0,0 +1,56 @@
SetPrototype = {
__index = {
union = function(self, other)
local res = Set{}
for k in pairs(self) do res[k] = true end
for k in pairs(other) do res[k] = true end
return res
end,
intersection = function(self, other)
local res = Set{}
for k in pairs(self) do res[k] = other[k] end
return res
end,
difference = function(self, other)
local res = Set{}
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
},
-- return string representation of set
__tostring = function(self)
-- list to collect all elements from the set
local l = {}
for k in pairs(self) do l[#l+1] = k end
return "{" .. table.concat(l, ", ") .. "}"
end,
-- allow concatenation with other types to yield string
__concat = function(a, b)
return (type(a) == 'string' and a or tostring(a)) ..
(type(b) == 'string' and b or tostring(b))
end
}
function Set(items)
local _set = {}
setmetatable(_set, SetPrototype)
for _, item in ipairs(items) do _set[item] = true end
return _set
end
A = Set{"John", "Serena", "Bob", "Mary", "Serena"}
B = Set{"Jim", "Mary", "John", "Jim", "Bob"}
print("Set A: " .. A)
print("Set B: " .. B)
print("\nSymm. difference (A\\B)(B\\A): " .. A:symmetric_difference(B))
print("Union AB : " .. A:union(B))
print("Intersection A∩B : " .. A:intersection(B))
print("Difference A\\B : " .. A:difference(B))
print("Difference B\\A : " .. B:difference(A))

View file

@ -0,0 +1,2 @@
A := {John, Bob, Mary, Serena};
B := {Jim, Mary, John, Bob};

View file

@ -0,0 +1 @@
symmdiff(A, B);

View file

@ -6,4 +6,7 @@
{'Serena'}
>>> setB - setA # elements in B that are not in A
{'Jim'}
>>>
>>> setA | setB # elements in A or B (union)
{'John', 'Bob', 'Jim', 'Serena', 'Mary'}
>>> setA & setB # elements in both A and B (intersection)
{'Bob', 'John', 'Mary'}

View file

@ -4,5 +4,4 @@
set(['Jim', 'Serena'])
>>> setA - setB # elements in A that are not in B
set(['Serena'])
>>> setB - setA # elements in B that are not in A
set(['Jim'])
>>> # and so on...

View file

@ -0,0 +1,10 @@
>>> setA.symmetric_difference(setB)
{'Jim', 'Serena'}
>>> setA.difference(setB)
{'Serena'}
>>> setB.difference(setA)
{'Jim'}
>>> setA.union(setB)
{'Jim', 'Mary', 'Serena', 'John', 'Bob'}
>>> setA.intersection(setB)
{'Mary', 'John', 'Bob'}

View file

@ -1,10 +1,4 @@
# with sets
require 'set'
a = Set["John", "Serena", "Bob", "Mary", "Serena"]
b = Set["Jim", "Mary", "John", "Jim", "Bob"]
# or, with arrays
a = ["John", "Serena", "Bob", "Mary", "Serena"]
b = ["Jim", "Mary", "John", "Jim", "Bob"]
a.uniq!
b.uniq!
# the union minus the intersection:
p sym_diff = (a | b)-(a & b) # => ["Serena", "Jim"]

View file

@ -1,3 +1,4 @@
a_not_b = a - b
b_not_a = b - a
sym_diff = a_not_b + b_not_a
require 'set'
a = Set["John", "Serena", "Bob", "Mary", "Serena"] #Set removes duplicates
b = Set["Jim", "Mary", "John", "Jim", "Bob"]
p sym_diff = a ^ b # => #<Set: {"Jim", "Serena"}>