Created
March 15, 2021 01:42
-
-
Save pamelafox/b72a858accdba7f7ae9277f622f6de66 to your computer and use it in GitHub Desktop.
link_iter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Link: | |
empty = () | |
def __init__(self, first, rest=empty): | |
assert rest is Link.empty or isinstance(rest, Link) | |
self.first = first | |
self.rest = rest | |
def __repr__(self): | |
if self.rest: | |
rest_repr = ', ' + repr(self.rest) | |
else: | |
rest_repr = '' | |
return 'Link(' + repr(self.first) + rest_repr + ')' | |
def __str__(self): | |
string = '<' | |
while self.rest is not Link.empty: | |
string += str(self.first) + ' ' | |
self = self.rest | |
return string + str(self.first) + '>' | |
def __iter__(self): | |
current = self | |
while current is not Link.empty: | |
yield current.first | |
current = current.rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment