Skip to main content

Functional Python Sequence

Project description

funct.Array

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.

Documentation

funct.Array is available on PyPi and can be installed with pip

$ pip install funct

Array Creation

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 pad method.

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 toArray method.

>>> Array(np.zeros(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))

Arrays provide static methods arange, linspace and logspace for creating linearly or logarithmically spaced Arrays.

Examples

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)))))

where a & b are Arrays and func1, func2 & func3 some functions.

Multiplying elements in a sequence with a constant
#  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)
Multiplying two sequences element-wise
#  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; add, pow, mod, gt, lt, etc.

Selecting values greater than some number
#  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)
Finding idex-wise maximum of sequences
>>> nums1 = Array(1, 2, 3, 4, 5)
>>> nums2 = Array(5, 4, 3, 2, 1)
>>> nums1.zip(nums2).map(max)
Array(5, 4, 3, 4, 5)
Splitting an Array based on type
>>> arr = 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'))
Multithreading/processing

Arrays also support parallel and concurrent execution. Functions applied to Arrays can be parallelized with the parmap and parstarmap methods. The same methods can be run asynchronously with the asyncmap and asyncstarmap methods.

>>> Array(1, 2, 3).parmap(some_heavy_func)
>>> Array(1, 2, 3).asyncmap(some_other_func)

Indexing

Array indexing is a combination of standard Python sequence indexing and numpy-style indexing. Array supports

  • Standard Python indexing (single element indexing, slicing)
  • Index arrays
  • Boolean masking
  • Multidimensional indexing

Examples

Standard Indexing
>>> a = Array(1, 2, 3)
>>> a[0]
1
>>> a[:2]
Array(1, 2)
Index Arrays
>>> a = Array('a', 'b', 'c', 'd')
>>> a[[1, 3]]
Array('b', 'd')
Boolean masking
>>> a = Array(1, 2, 3, 4)
>>> a[[True, False, False, True]]
Array(1, 4)
Multidimensional indexing
>>> a = Array((1, 2), (3, 4), (5, 6))
>>> a[:, 0]
Array(1, 3, 5)

Note that when indexing 'ragged' nested Arrays multidimensional indexing may raise an IndexError, since Array does not care whether all the nested Arrays are the same size, as opposed to numpy ndarrays.

Full documentation available here.

Notes

  • Mathematical operations such as addition or multiplication can be done with the add and mul methods, not with the + and * operators to avoid confusion and to retain the behaviour of the built-in list.
  • Inplace operations are postfixed with an underscore (e.g. arr.abs_). However, methods for adding elements to Arrays (append, extend, insert, etc.) are inplace by default. (Note: To be changed. In the next release the operations are inplace if inplace=True is passed to the methods.)
  • Inplace operators are generally faster than out of place operations.
  • Even though Array preserves nearly the same functionality as the built-in list, there are a few differences in their behaviour, the most important of which are
    • == (__eq__) Returns element-wise comparison.
    • bool (__bool__) Returns whether all elements evaluate to True.
    • Arrays are hashable. Note that this is implemented by using the Array's tuple representation in __hash__.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Funct-0.9.2.tar.gz (43.6 kB view details)

Uploaded Source

Built Distribution

Funct-0.9.2-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file Funct-0.9.2.tar.gz.

File metadata

  • Download URL: Funct-0.9.2.tar.gz
  • Upload date:
  • Size: 43.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.4

File hashes

Hashes for Funct-0.9.2.tar.gz
Algorithm Hash digest
SHA256 13f8c95f154f7b81083411d1e37ea6e988a032c22cd5853bf176dcfc0221695e
MD5 dfd11edb2812547875bba18851075868
BLAKE2b-256 5084afbda92d2146deb841d48d3b4da8cef4b1c34ce855eed409ab0cf3133125

See more details on using hashes here.

File details

Details for the file Funct-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: Funct-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.4

File hashes

Hashes for Funct-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0d838556b373454a4a8d6c561ecc19900d2392ed754bfc5ba24cd3b94782216d
MD5 7205278955751db9bf03802e79ec1ecc
BLAKE2b-256 b109907ce13186b92cd5054a37a68e1086781c22ed4df5a9b1a17a17d4de77bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page