Array is a functional mutable sequence inheriting from Python's built-in list. Array provides 100+ higher-order methods and more functionality to the built-in list, making operations on sequences simpler and one-liners neater with no third party packages required.
Array provides a combination of python built-ins, features found in NumPy arrays, and higher-order methods common to functional languages without the weird semantics of the builtins, still preserving the same functionality and the dynamic nature of the built-in list.
funct.Array is available on PyPi and can be installed with pip
pip install funct
Arrays can be created either with multiple arguments or by providing a sequence as an argument.
from funct import Array Array(1, 2, 3) Array(1, 2, 3) Array([1, 2, 3]) Array(1, 2, 3)
An Array can also be initialized with the static
zeros method or the
Python built-in sequences (including nested ones) lists, tuples and ranges are converted to
Arrays on instantiation. However, other iterables e.g. generators and numpy ndarrays
are converted to Arrays only if the argument consists of a single iterable. The elements
can be converted to Arrays by calling the
3)) Array(0.0, 0.0, 0.0) Array(np.zeros(3), np.zeros(3)) Array(array([0., 0., 0.]), array([0., 0., 0.]) Array(np.zeros(3), np.zeros(3)).toArray() Array(Array(0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0))Array(np.zeros(
Arrays provide static methods
creating linearly or logarithmically spaced Arrays.
Chaining multiple functions with Arrays result in cleaner code without multiple nested functions, e.g.
a.zip(b).map(func1).filter(func2).forall(func3) # vs. in traditional python all(map(func3, filter(func2, map(func1, zip(a, b)))))
b are Arrays and
func3 some functions.
# In traditional python the multiplication could be implemented using list comprehensions as follows nums = [1, 2, 3, 4, 5] [a * 10 for a in nums] [10, 20, 30, 40, 50] # With Arrays multiplication simplifies to from funct import Array nums = Array(nums) nums.mul(10) Array(10, 20, 30, 40, 50)
# Traditional python nums2 = [11, 12, 13, 14, 15] [a * b for a, b in zip(nums, nums2)] [11, 24, 39, 56, 75] # With Arrays nums.mul(nums2) Array(11, 24, 39, 56, 75)
Same syntax applies for all mathematical operators;
# Traditional python n = 2 nums1 = [1, 2, 3, 4, 5] [x for x in nums if x > n] [3, 4, 5] # With Arrays nums[nums > n] Array(3, 4, 5)
1, 2, 3, 4, 5) nums2 = Array(5, 4, 3, 2, 1) nums1.zip(nums2).map(max) Array(5, 4, 3, 4, 5)nums1 = Array(
1, 2, "a", "b") arr.groupBy(type)[:, 1] # group by type and select the 2nd element of the tuples Array(Array(1, 2), Array('a', 'b'))arr = Array(
Arrays also support parallel and concurrent execution.
Functions applied to Arrays can be parallelized with the
parstarmap methods. The same methods can be run asynchronously with the
1, 2, 3).parmap(some_heavy_func) Array(1, 2, 3).asyncmap(some_other_func)Array(
Array indexing is a combination of standard Python sequence indexing and numpy-style indexing. Array supports
1, 2, 3) a 1 a[:2] Array(1, 2)a = Array(
'a', 'b', 'c', 'd') a[[1, 3]] Array('b', 'd')a = Array(
1, 2, 3, 4) a[[True, False, False, True]] Array(1, 4)a = Array(
1, 2), (3, 4), (5, 6)) a[:, 0] Array(1, 3, 5)a = Array((
Note that when indexing 'ragged' nested Arrays multidimensional indexing may
IndexError, since Array does not care whether all the nested Arrays are
the same size, as opposed to numpy ndarrays.
mulmethods, not with the
*operators to avoid confusion and to retain the behaviour of the built-in list.
arr.abs_). However, methods for adding elements to Arrays (
insert, etc.) are inplace by default. (Note: To be changed. In the next release the operations are inplace if
inplace=Trueis passed to the methods.)
__eq__) Returns element-wise comparison.
__bool__) Returns whether all elements evaluate to True.