class Set: def __init__(self, el = None): self.contents = list() if el != None: for e in el: self += e def __iadd__(self, item): # (+=) add element to set if item not in self.contents: self.contents.append(item) return self def __isub__(self, item): # (-=) remove element from set if item in self.contents: self.contents.remove(item) return self def __or__(self, other): # (|) set union rtn = Set(self.contents) for e in other: rtn += e return rtn def __and__(self, other): # (&) set intersection rtn = Set() for e in self: if e in other: rtn += e return rtn def __sub__(self, other): # (-) set difference rtn = Set() for e in self: if e not in other: rtn += e return rtn def __str__(self): rtn = str(self.contents) return "{" + rtn[1:len(rtn)-1] + "}" def __iter__(self): i = 0 limit = len(self.contents) while i < limit: yield self.contents[i] i += 1 def forAll(self, f): for e in self: f(e) def map(self, f): rtn = Set() for e in self: rtn += f(e) return rtn