def test_iter(): c = Cons(12, Cons('banana', Cons(dir))) i = iter(c) assert next(i) == 12 assert next(i) == 'banana' assert next(i) == dir with raises(StopIteration): next(i)
def parse_etuple(self): if self.head() == '(': self._next() car = self.parse_etuple() self._close() return Cons(car, self.parse_etuple()) # <---<< cons elif self.head() == ')': return Symbol.NIL # <---<< empty list else: car = Symbol.get(self.head()) self._next() return Cons(car, self.parse_etuple()) # <---<< cons
def _append(self, car): if self.current_node.car is None: self.current_node.car = car else: new_node = Cons(car, None) self.current_node.cdr = new_node self.current_node = new_node
class List(object): """A simple linked-list class. A wrapper for cons-based list construction.""" __slots__ = ('_list') def __init__(self, seq=None): """Construct a list, possibly from seq.""" self._list = None # an empty list if seq is not None: for item in seq: self.append(item) def pop(self): """Remove and return last element of list.""" if self.isEmpty(): raise IndexError("Pop from an empty list.") elif self._list.isLast(): # has 1 element? value = self._list.first self._list = None return value else: return self._list.pop() def isEmpty(self): """Return True exactly when list is empty.""" return self._list is None def append(self, value): """Add an element to the end of the list.""" if self.isEmpty(): self._list = Cons(value) else: self._list.append(value) def extend(self, seq): """Append elements of the sequence.""" for item in seq: self.append(item) def reverse(self): if self._list: self._list = self._list.reverse() def __contains__(self, value): if self.isEmpty(): return False else: return value in self._list def __len__(self): if self.isEmpty(): return 0 else: return len(self._list) def __eq__(self, other): if (not isinstance(other, List)) or (len(self) != len(other)): return False # assertion: they're the same length List if self.isEmpty(): return True else: return self._list == other._list def __getitem__(self, n): if n >= len(self): raise IndexError("List index of {} out of range {}".format( n.len(self))) else: return self._list[n] def __setitem__(self, n, value): if n >= len(self): raise IndexError("List index of {} out of range {}".format( n.len(self))) else: self._list[n] = value def __iter__(self): if self._list: for item in self._list: yield item def __str__(self): if self.isEmpty(): return "[]" else: return str(self._list) def __repr__(self): if self.isEmpty(): return "List()" else: return "List({})".format(self)
def append(self, value): """Add an element to the end of the list.""" if self.isEmpty(): self._list = Cons(value) else: self._list.append(value)
def test_getitem(value): c = Cons(value) assert c[0] == value
def cons_from_sequence(data): rdata = iter(reversed(data)) c = None for item in rdata: c = Cons(item, c) return c
def test_get_item_single(value): c = Cons(value) assert c[0] == value
def open_list(self, line): inner_node = Cons(None, None, line=line) outer_node = Cons(inner_node, None) self.current_node.cdr = outer_node self.stack.append(outer_node) self.current_node = inner_node
def __init__(self): self.current_node = Cons(Symbol('.p'), None) self.root = self.current_node self.stack = deque()
def cons(a, b): from cons import Cons return Cons(a, b)