Skip to main content

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


Download files

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

Source Distribution

better_functools-0.2.0.tar.gz (36.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

better_functools-0.2.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

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

Hashes for better_functools-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1e8c0d0dedb7e6abad435248dd35025e64d2071138bc5512b6b804c215b92236
MD5 4b2264a0898dd2bb7d2e0d36e4a84c00
BLAKE2b-256 c20b4263253de9f86ba2e4b2629e516883de880fa89a6354f24f8120f7887391

See more details on using hashes here.

Provenance

The following attestation bundles were made for better_functools-0.2.0.tar.gz:

Publisher: python-publish.yml on Jamie-Chang/better-functools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file better_functools-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for better_functools-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc5fb96bd87f641ee8b8b4dbdc7b0763c1941fd977607909a19b817319cf2f44
MD5 ce04276a40687054f3f167ff201d2172
BLAKE2b-256 e46a6afcf47aea02606bbc76350e8c00a3b96b71b67b632d67a4b6cddeef1bde

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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