Skip to content

Instantly share code, notes, and snippets.

@tjarratt
Created July 19, 2023 17:38
Show Gist options
  • Save tjarratt/d46b3d03bbe98c1116b98b7c3687ecf0 to your computer and use it in GitHub Desktop.
Save tjarratt/d46b3d03bbe98c1116b98b7c3687ecf0 to your computer and use it in GitHub Desktop.
A set that can be iterated and knows its size
class ListSet:
def __init__(self):
self._values = []
self.__index = 0
def add(self, value):
if value in self._values:
return
self._values.append(value)
def __iter__(self):
return self
def __next__(self):
if self.__index >= len(self._values):
raise StopIteration()
val = self._values[self.__index]
self.__index += 1
return val
def __len__(self):
return len(self._values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment