Skip to main content

Functional collections extension functions for Python

Project description

pyfuncol

CI codecov PyPI Downloads Documentation Status GitHub license

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

Note: If you are not using forbiddenfruit, the functions will not extend the builtins. Please see here for usage without forbiddenfruit.

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}

Usage without forbiddenfruit

If you are using a Python intepreter other than CPython, forbiddenfruit will not work.

Fortunately, if forbiddenfruit does not work on your installation or if you do not want to use it, pyfuncol also supports direct function calls without extending builtins.

from pyfuncol import list as pfclist

pfclist.map([1, 2, 3], lambda x: x * 2)
# [2, 4, 6]

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

For functions to extend built-ins, Forbidden Fruit is necessary (CPython only).

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

pyfuncol-1.2.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

pyfuncol-1.2.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file pyfuncol-1.2.1.tar.gz.

File metadata

  • Download URL: pyfuncol-1.2.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for pyfuncol-1.2.1.tar.gz
Algorithm Hash digest
SHA256 09702ab228db83669f757b03a446997c730167d5ed570e852da31e869d33c0e5
MD5 d7edfcfadb04c0a1aab17d503fd4c271
BLAKE2b-256 fa98c6cd75a1376d04dc71ab82c003118da927995dbf13b7160b2c834ffb6a24

See more details on using hashes here.

File details

Details for the file pyfuncol-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyfuncol-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for pyfuncol-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2587ad97f016bd6d6221d3b9cc945e46ded4942cb24a57f42e39e9578f17b52e
MD5 c1b5a092d751b60de357daa93b4d7b20
BLAKE2b-256 72326cb0ae44d7934a5f045de57548b7e3962c8cf8655d98231f7c58f20ca85f

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