1.1 --- a/lib/__builtins__/list.py Sat Jan 28 23:32:15 2017 +0100
1.2 +++ b/lib/__builtins__/list.py Sun Jan 29 00:18:52 2017 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 List objects.
1.6
1.7 -Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -22,7 +22,7 @@
1.13 from __builtins__.iterator import itemiterator
1.14 from __builtins__.sequence import sequence
1.15 from native import list_append, list_concat, list_element, list_init, \
1.16 - list_len, list_nonempty, list_setelement
1.17 + list_len, list_nonempty, list_setelement, list_setsize
1.18
1.19 class list(sequence):
1.20
1.21 @@ -59,8 +59,19 @@
1.22 for i in iterable:
1.23 self.append(i)
1.24
1.25 - def pop(self): pass
1.26 + def pop(self):
1.27 +
1.28 + "Remove the last item from the list, returning the item."
1.29 +
1.30 + i = self[-1]
1.31 +
1.32 + # NOTE: Should truncate the allocated list after several pops.
1.33 +
1.34 + list_setsize(self.__data__, self.__len__() - 1)
1.35 + return i
1.36 +
1.37 def reverse(self): pass
1.38 +
1.39 def sort(self, cmp=None, key=None, reverse=0): pass
1.40
1.41 def __len__(self):
1.42 @@ -69,7 +80,13 @@
1.43
1.44 return list_len(self.__data__)
1.45
1.46 - def __add__(self, other): pass
1.47 + def __add__(self, other):
1.48 +
1.49 + "Add this list to 'other', producing a new list."
1.50 +
1.51 + l = list(self)
1.52 + l.extend(other)
1.53 + return l
1.54
1.55 def __iadd__(self, other):
1.56
2.1 --- a/tests/list.py Sat Jan 28 23:32:15 2017 +0100
2.2 +++ b/tests/list.py Sun Jan 29 00:18:52 2017 +0100
2.3 @@ -42,5 +42,28 @@
2.4 except ValueError, exc:
2.5 print "l.index(4): failed to find argument", exc.value
2.6
2.7 +# Test equality.
2.8 +
2.9 print l == [1, 2, 3] # False
2.10 print l == [1, 2, 3, "four"] # True
2.11 +
2.12 +# Test concatenation.
2.13 +
2.14 +a = [1, 2]
2.15 +a += [3, 4]
2.16 +print a # [1, 2, 3, 4]
2.17 +
2.18 +b = [5, 6]
2.19 +c = a + b
2.20 +print c # [1, 2, 3, 4, 5, 6]
2.21 +
2.22 +# Test removal.
2.23 +
2.24 +print c.pop() # 6
2.25 +print c # [1, 2, 3, 4, 5]
2.26 +
2.27 +d = []
2.28 +try:
2.29 + d.pop() # should raise an exception
2.30 +except IndexError, exc:
2.31 + print "d.pop(): failed to access item", exc.index