# HG changeset patch # User Paul Boddie # Date 1485645532 -3600 # Node ID 360560918d918a58cb1775ae83234ff7dc639900 # Parent 626c0f5435707544920c0056af370b49f435c4ae Implemented list pop and addition methods. diff -r 626c0f543570 -r 360560918d91 lib/__builtins__/list.py --- a/lib/__builtins__/list.py Sat Jan 28 23:32:15 2017 +0100 +++ b/lib/__builtins__/list.py Sun Jan 29 00:18:52 2017 +0100 @@ -3,7 +3,7 @@ """ List objects. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -22,7 +22,7 @@ from __builtins__.iterator import itemiterator from __builtins__.sequence import sequence from native import list_append, list_concat, list_element, list_init, \ - list_len, list_nonempty, list_setelement + list_len, list_nonempty, list_setelement, list_setsize class list(sequence): @@ -59,8 +59,19 @@ for i in iterable: self.append(i) - def pop(self): pass + def pop(self): + + "Remove the last item from the list, returning the item." + + i = self[-1] + + # NOTE: Should truncate the allocated list after several pops. + + list_setsize(self.__data__, self.__len__() - 1) + return i + def reverse(self): pass + def sort(self, cmp=None, key=None, reverse=0): pass def __len__(self): @@ -69,7 +80,13 @@ return list_len(self.__data__) - def __add__(self, other): pass + def __add__(self, other): + + "Add this list to 'other', producing a new list." + + l = list(self) + l.extend(other) + return l def __iadd__(self, other): diff -r 626c0f543570 -r 360560918d91 tests/list.py --- a/tests/list.py Sat Jan 28 23:32:15 2017 +0100 +++ b/tests/list.py Sun Jan 29 00:18:52 2017 +0100 @@ -42,5 +42,28 @@ except ValueError, exc: print "l.index(4): failed to find argument", exc.value +# Test equality. + print l == [1, 2, 3] # False print l == [1, 2, 3, "four"] # True + +# Test concatenation. + +a = [1, 2] +a += [3, 4] +print a # [1, 2, 3, 4] + +b = [5, 6] +c = a + b +print c # [1, 2, 3, 4, 5, 6] + +# Test removal. + +print c.pop() # 6 +print c # [1, 2, 3, 4, 5] + +d = [] +try: + d.pop() # should raise an exception +except IndexError, exc: + print "d.pop(): failed to access item", exc.index