예제 #1
0
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)
예제 #2
0
파일: read2.py 프로젝트: cohadar/lisping
 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
예제 #3
0
 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
예제 #4
0
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)
예제 #5
0
 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)
예제 #6
0
def test_getitem(value):
    c = Cons(value)
    assert c[0] == value
예제 #7
0
def cons_from_sequence(data):
    rdata = iter(reversed(data))
    c = None
    for item in rdata:
        c = Cons(item, c)
    return c
예제 #8
0
def test_get_item_single(value):
    c = Cons(value)
    assert c[0] == value
예제 #9
0
 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
예제 #10
0
 def __init__(self):
     self.current_node = Cons(Symbol('.p'), None)
     self.root = self.current_node
     self.stack = deque()
예제 #11
0
def cons(a, b):
    from cons import Cons
    return Cons(a, b)