A functional mutable sequence inheriting from the built-in list.
| 24 | |
| 25 | |
| 26 | class Array(list): |
| 27 | """ |
| 28 | A functional mutable sequence |
| 29 | inheriting from the built-in list. |
| 30 | """ |
| 31 | |
| 32 | __baseIterables = (list, range, tuple) |
| 33 | |
| 34 | def __init__(self, *args): |
| 35 | |
| 36 | """ |
| 37 | Constructs an Array from arguments. |
| 38 | Nested lists, tuples and range objects are converted to |
| 39 | Arrays, while other iterables will stored as Array elements. |
| 40 | """ |
| 41 | if len(args) == 1 and isinstance(args[0], Iterable): |
| 42 | args = list(args[0]) |
| 43 | |
| 44 | if any(map(lambda e: isinstance(e, Array.__baseIterables), args)): |
| 45 | super().__init__(self.__convert(a) for a in args) |
| 46 | else: |
| 47 | super().__init__(args) |
| 48 | |
| 49 | def add(self, e, inplace=False): |
| 50 | """ |
| 51 | Element-wise addition with given scalar or sequence. |
| 52 | """ |
| 53 | return self.__operate(operator.add, e, inplace) |
| 54 | |
| 55 | def add_(self, e): |
| 56 | """ |
| 57 | Inplace element-wise addition with given scalar or sequence. |
| 58 | """ |
| 59 | warn() |
| 60 | self[:] = self.__operate(operator.add, e, True) |
| 61 | return self |
| 62 | |
| 63 | def sub(self, e, inplace=False): |
| 64 | """ |
| 65 | Element-wise subtraction with given scalar or sequence. |
| 66 | """ |
| 67 | return self.__operate(operator.sub, e, inplace) |
| 68 | |
| 69 | def sub_(self, e): |
| 70 | """ |
| 71 | Inplace element-wise subtraction with given scalar or sequence. |
| 72 | """ |
| 73 | warn() |
| 74 | self[:] = self.__operate(operator.sub, e, True) |
| 75 | return self |
| 76 | |
| 77 | def mul(self, e, inplace=False): |
| 78 | """ |
| 79 | Element-wise multiplication with given scalar or sequence. |
| 80 | """ |
| 81 | return self.__operate(operator.mul, e, inplace) |
| 82 | |
| 83 | def mul_(self, e): |