1.1 --- a/lib/__builtins__/tuple.py Mon Jul 16 22:20:57 2018 +0200
1.2 +++ b/lib/__builtins__/tuple.py Mon Jul 16 23:00:47 2018 +0200
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Tuple objects.
1.6
1.7 -Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015, 2016, 2017, 2018 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,8 @@
1.13 from __builtins__.iteration.iterator import itemiterator
1.14 from __builtins__.sequence import hashable, sequence
1.15 from native import tuple_init, \
1.16 - list_element, list_len, list_setsize, list_setelement
1.17 + list_element, list_len, list_setsize, list_setelement, \
1.18 + isinstance as _isinstance
1.19
1.20 class tuple(sequence, hashable):
1.21
1.22 @@ -73,7 +74,13 @@
1.23
1.24 return list_len(self.__data__)
1.25
1.26 - def __add__(self, other): pass
1.27 + def __add__(self, other):
1.28 +
1.29 + "Add this tuple to 'other'."
1.30 +
1.31 + if not _isinstance(other, tuple):
1.32 + raise TypeError
1.33 + return tuple(tuplepair(self, other))
1.34
1.35 def __str__(self):
1.36
1.37 @@ -110,4 +117,28 @@
1.38
1.39 raise TypeError
1.40
1.41 +class tuplepair:
1.42 +
1.43 + "A combination of tuples."
1.44 +
1.45 + def __init__(self, a, b):
1.46 + self.a = a
1.47 + self.b = b
1.48 +
1.49 + def __len__(self):
1.50 +
1.51 + "Return the combined length of the tuples."
1.52 +
1.53 + return len(self.a) + len(self.b)
1.54 +
1.55 + def __getitem__(self, index):
1.56 +
1.57 + "Return the value from 'index' in the combined tuple."
1.58 +
1.59 + asize = len(self.a)
1.60 + if index < asize:
1.61 + return self.a.__get_single_item__(index)
1.62 + else:
1.63 + return self.b.__get_single_item__(index - asize)
1.64 +
1.65 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/tests/tuple.py Mon Jul 16 22:20:57 2018 +0200
2.2 +++ b/tests/tuple.py Mon Jul 16 23:00:47 2018 +0200
2.3 @@ -31,3 +31,19 @@
2.4 a, b, c = l
2.5 except ValueError, exc:
2.6 print "a, b, c = l: failed with length", exc.value
2.7 +
2.8 +# Add tuples together.
2.9 +
2.10 +m = ("five", 6, 7, 8)
2.11 +n = t + m
2.12 +print n # (1, 2, 3, "four", "five", 6, 7, 8)
2.13 +
2.14 +# Add to list, really testing tuple iteration.
2.15 +
2.16 +o = l + m
2.17 +print o # [1, 2, 3, "four", "five", 6, 7, 8]
2.18 +
2.19 +try:
2.20 + print t + l # should raise an exception
2.21 +except TypeError:
2.22 + print "t + l: failed due to tuple-list addition"