Functional Programming Ergonomics in Python.
Project description
better-functools
Better functools improves functional programming ergonomics in Python
Installation
$ pip install better-functools
Background
The library introduces 2 different operators for use with functions:
|which we'll call "pipe"@which we'll call "apply"
The following is an example:
Pipeline(inputs)
| func(itertools.combinations @ func.arg(Iterable[int]) @ bind(2))
| filter @ bind(sum @ compose(eq @ bind(2020)))
| map @ bind(prod)
| sum
| Pipeline.unwrap
This may look strange but we can break this down.
pipe
Pipeline wraps inputs and enables unix like pipes.
So Pipeline(v) | fn is equal to Pipeline(fn(v)).
| Pipeline.unwrap is finally used to unwrap the Pipeline and extract the value.
apply
better_functools.apply includes a number of functions that implements @.
For example, map @ bind(prod) means bind prod to the first arg of map.
More apply functions can be called directly too, the above expression is equivalent to
bind(prod)(map).
The usual pattern is: some_value @ some_operator(args)
The @ operator takes some getting used to, but the main benefit is how chainable operators are.
add @ bind(1) @ bind(2)
reads left to right fairly well, but if we were to call directly:
bind(2)(bind(1)(add))
It's just a lot harder to read and we need more brackets.
Putting @ and | together
You can see that @ and | both facilitate some sort of "function chaining". So why add 2
different operators?
@ is higher precedence than | so it can be used in each stage of a pipe without brackets.
Generally the idea is to use @ operators to build the function for each stage of a |.
Without @ functions are a lot harder to build inline and often must be defined explicitly
elsewhere.
General Tips
Type Checking
- The typing here is built for Pyright.
- Mypy compatibility is okay, specific issues with mypy are highlighted in function docs.
- Other type checkers simply lack the popularity for me to support actively.
functools.partial
Feel free to use functools.partial to create partial functions in place of bind and func.
Both MyPy and Pyright has a custom type checking handler for partial. This is the best way to
bind keyword arguments right now.
partial(itertools.combinations, r=2)
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file better_functools-0.2.0.tar.gz.
File metadata
- Download URL: better_functools-0.2.0.tar.gz
- Upload date:
- Size: 36.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e8c0d0dedb7e6abad435248dd35025e64d2071138bc5512b6b804c215b92236
|
|
| MD5 |
4b2264a0898dd2bb7d2e0d36e4a84c00
|
|
| BLAKE2b-256 |
c20b4263253de9f86ba2e4b2629e516883de880fa89a6354f24f8120f7887391
|
Provenance
The following attestation bundles were made for better_functools-0.2.0.tar.gz:
Publisher:
python-publish.yml on Jamie-Chang/better-functools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
better_functools-0.2.0.tar.gz -
Subject digest:
1e8c0d0dedb7e6abad435248dd35025e64d2071138bc5512b6b804c215b92236 - Sigstore transparency entry: 177016336
- Sigstore integration time:
-
Permalink:
Jamie-Chang/better-functools@0de7e87e26a5a5e05132f96a1076dae393ca24c2 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Jamie-Chang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0de7e87e26a5a5e05132f96a1076dae393ca24c2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file better_functools-0.2.0-py3-none-any.whl.
File metadata
- Download URL: better_functools-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc5fb96bd87f641ee8b8b4dbdc7b0763c1941fd977607909a19b817319cf2f44
|
|
| MD5 |
ce04276a40687054f3f167ff201d2172
|
|
| BLAKE2b-256 |
e46a6afcf47aea02606bbc76350e8c00a3b96b71b67b632d67a4b6cddeef1bde
|
Provenance
The following attestation bundles were made for better_functools-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on Jamie-Chang/better-functools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
better_functools-0.2.0-py3-none-any.whl -
Subject digest:
bc5fb96bd87f641ee8b8b4dbdc7b0763c1941fd977607909a19b817319cf2f44 - Sigstore transparency entry: 177016337
- Sigstore integration time:
-
Permalink:
Jamie-Chang/better-functools@0de7e87e26a5a5e05132f96a1076dae393ca24c2 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Jamie-Chang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0de7e87e26a5a5e05132f96a1076dae393ca24c2 -
Trigger Event:
release
-
Statement type: