Here's a very basic Set class in Falcon.
/* ================================
= A very basic Set implementation
= for Falcon PL.
= Source: a Python script I've found
================================= */
class Set(sequence)
seq = sequence
elems = []
init
if self.seq
for elem in self.seq
if elem notin self.elems
self.elems += elem
end
end
end
end
function toString()
if len(self.elems) == 0
return "{-o-}"// Maybe try "{\x2205}" (Unicode char for an empty set)
else
return "{" + ",".merge(map({x => x.toString()},self.elems)) + "}"
end
end
function copy()
// Shallow copy of a set object.
return Set(self.elems)
end
function contains(elem)
return elem in self.elems
end
function num()
return len(self.elems)
end
function items()
// Returns a list of the elements in the set.
return self.elems
end
function add(elem)
// Add one element to the set.
if elem notin self.elems
self.elems += elem
end
end
function sub(elem)
// Remove an element from the set. Return an error if elem is not in the set."""
self.elems -= elem
end
function discard(elem)
// Remove an element from the set. Do nothing if elem is not in the set.
self.elems -= elem
end
// The basic binary operations with sets.
function union(other)
// Union of two sets.
ret = self.copy()
for elem in other.elems
if elem notin ret.elems
ret.elems += elem
end
end
return ret
end
function diff(other)
// Difference of two sets.
ret = self.copy()
for elem in other.elems
ret.discard(elem)
end
return ret
end
function inter(other)
// Intersection of two sets.
ret = Set()
for elem in self.elems
if elem in other.elems
ret.elems += elem
end
end
return ret
end
function symdiff(other)
// Symmetric difference of two sets.
ret = Set()
temp = other.copy()
for elem in self.elems
if elem in temp.elems
temp.elems -= elem
else
ret.elems += elem
end
end
// Add remaining elements.
for elem in temp.elems
ret.elems += elem
end
return ret
end
function cartprod(other)
// Returns the cartesian product of self with other
ret = Set()
for elemself in self.elems
for elemother in other.elems
ret.add(Set([elemself, elemother]))
end
end
return ret
end
// Some of the binary comparisons.
function strickly_contained(other)
// Returns 1 if the lhs set is contained but not equal to the rhs set.
if len(self.elems) < len(other.elems)
temp = other.copy()
for elem in self.elems
if elem in temp.elems
temp -= elem
else
return 0
end
end
if len(temp.elems) == 0
return 1
end
else
return 0
end
end
function contained(other)
// Returns 1 if the lhs set is contained in the rhs set.
if len(self.elems) <= len(other.elems)
ret = 1
for elem in self.elems
if elem notin other.elems
ret = 0
break
end
end
return ret
else
return 0
end
end
function equal(other)
// Returns 1 if the sets are equal.
if len(self.elems) != len(other.elems)
return 0
else
if len(self - other) == 0
return 1
end
end
end
function rel_complement(B,A)
// Ensemble des x dans B qui ne sont pas dans A
res = Set([])
for x in B.elems
if x notin A.elems
res.add(x)
end
end
return res
end
function fet(elem, T)
// http://en.wikipedia.org/wiki/Power_set#Algorithms
// return the set with the element e added to each set X in T.
// each of T element must be a Set !
res = Set([])
for X in T.elems
res.add( X.union( Set([elem]) ) )
end
return res
end
function powersets(L)
// See http://en.wikipedia.org/wiki/Power_set#Algorithms
if len(L.elems) == 0
return Set([Set([])])
end
e = L.elems[0]
T = self.rel_complement(L,Set([e]))
return self.powersets(T).union( self.fet(e, self.powersets(T)) )
end
function combin(k)
return Set( filter( { x => len(x.elems) == k}, self.powersets(self).elems) )
end
end
// Some testing
s1 = Set([1,2,3,4,2]) // notice that the '2' element is twice here
s2 = Set([4,5,6,2,7,8,9])
s3 = Set([])
> "set1: ",s1
> "set2: ",s2
> "set3: ",s3
>
> "set1 inter set2: ",s1.inter(s2)
> "set1 union set2: ",s1.union(s2)
>
> "Removing element 3 from Set1"
s1.discard(3)
> "set1: ",s1
>
> "Adding the \"a\" element to Set1"
s1.add("a")
> "set1: ",s1
> "Len(set1)=", s1.num()
> "Cartesian product set1 x set2: "
> s1.cartprod(s2)
>
// Sample from http://en.wikipedia.org/wiki/Exact_cover_problem#Detailed_example
s0 = Set(["x","y","z"])
> "The power sets of s0=",s0, " are:"
> s0.powersets(s0)
>
> "Combination of 0 elements in s0=",s0, " is:"
> s0.combin(0)
>
> "Combination of 1 elements in s0=",s0, " is:"
> s0.combin(1)
>
> "Combination of 2 elements in s0=",s0, " is:"
> s0.combin(2)
>
> "Combination of 3 elements in s0=",s0, " is:"
> s0.combin(3)
>
/* OUTPUTS
set1: {1,2,3,4}
set2: {4,5,6,2,7,8,9}
set3: {-o-}
set1 inter set2: {2,4}
set1 union set2: {1,2,3,4,5,6,7,8,9}
Removing element 3 from Set1
set1: {1,2,4}
Adding the "a" element to Set1
set1: {1,2,4,a}
Len(set1)=4
Cartesian product set1 x set2:
{{1,4},{1,5},{1,6},{1,2},{1,7},{1,8},{1,9},{2,4},{2,5},{2,6},{2},{2,7},{2,8},{2,
9},{4},{4,5},{4,6},{4,2},{4,7},{4,8},{4,9},{a,4},{a,5},{a,6},{a,2},{a,7},{a,8},{
a,9}}
The power sets of s0={x,y,z} are:
{{-o-},{z},{y},{z,y},{x},{z,x},{y,x},{z,y,x}}
Combination of 0 elements in s0={x,y,z} is:
{{-o-}}
Combination of 1 elements in s0={x,y,z} is:
{{z},{y},{x}}
Combination of 2 elements in s0={x,y,z} is:
{{z,y},{z,x},{y,x}}
Combination of 3 elements in s0={x,y,z} is:
{{z,y,x}}
*/