Functional collections extension functions for Python
Project description
pyfuncol
A Python functional collections library. It extends collections built-in types with useful methods to write functional Python code. It uses Forbidden Fruit under the hood.
Installation
pip install pyfuncol
Usage
To use the methods, you just need to import pyfuncol. Some examples:
import pyfuncol
[1, 2, 3, 4].map(lambda x: x * 2).filter(lambda x: x > 4)
# [6, 8]
[1, 2, 3, 4].fold_left(0, lambda acc, n: acc + n)
# 10
{1, 2, 3, 4}.map(lambda x: x * 2).filter_not(lambda x: x <= 4)
# {6, 8}
["abc", "def", "e"].group_by(lambda s: len(s))
# {3: ["abc", "def"], 1: ["e"]}
{"a": 1, "b": 2, "c": 3}.flat_map(lambda kv: {kv[0]: kv[1] ** 2})
# {"a": 1, "b": 4, "c": 9}
pyfuncol provides parallel operations (for now par_map
, par_flat_map
, par_filter
and par_filter_not
):
[1, 2, 3, 4].par_map(lambda x: x * 2).par_filter(lambda x: x > 4)
# [6, 8]
{1, 2, 3, 4}.par_map(lambda x: x * 2).par_filter_not(lambda x: x <= 4)
# {6, 8}
{"a": 1, "b": 2, "c": 3}.par_flat_map(lambda kv: {kv[0]: kv[1] ** 2})
# {"a": 1, "b": 4, "c": 9}
pyfuncol provides operations leveraging memoization to improve performance (for now pure_map
, pure_flat_map
, pure_filter
and pure_filter_not
). These versions work only for pure functions (i.e., all calls to the same args return the same value) on hashable inputs:
[1, 2, 3, 4].pure_map(lambda x: x * 2).pure_filter(lambda x: x > 4)
# [6, 8]
{1, 2, 3, 4}.pure_map(lambda x: x * 2).pure_filter_not(lambda x: x <= 4)
# {6, 8}
{"a": 1, "b": 2, "c": 3}.pure_flat_map(lambda kv: {kv[0]: kv[1] ** 2})
# {"a": 1, "b": 4, "c": 9}
API
For lists, please refer to the docs.
For dictionaries, please refer to the docs.
For sets and frozensets, please refer to the docs.
For more details, please have a look at the API reference.
We support all subclasses with default constructors (OrderedDict
, for example).
Documentation
See https://pyfuncol.readthedocs.io/.
Compatibility
Since it depends on Forbidden Fruit, it only works on CPython.
Contributing
See the contributing guide for detailed instructions on how to get started with the project.
License
pyfuncol is licensed under the MIT license.
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
Built Distribution
File details
Details for the file pyfuncol-1.2.tar.gz
.
File metadata
- Download URL: pyfuncol-1.2.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a6171e1cc91df2b17e2b37d0ab1e92cf4f4aedfb57da8445e573a64185debbf |
|
MD5 | e9952789d31424f4b7d80a9cac433f5e |
|
BLAKE2b-256 | ef65261529e268858a151f9e6ce7255337e2871b72e5a36be370ba9d99156133 |
File details
Details for the file pyfuncol-1.2-py3-none-any.whl
.
File metadata
- Download URL: pyfuncol-1.2-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6063132298dcacb4bdd2fa42e3e30c5825cc6845b80cd58d13b5090feee0ebd6 |
|
MD5 | dff2c191c9a1fd7f52e2ad70cbe5e53b |
|
BLAKE2b-256 | ca40cf7fd5077a8cb07d65672fa35c017ba57c23be89c14d15cad6de109e4f4e |