Skip to main content

Partial function application with '...'

Project description

partiell

Partial argument application using ....

Description

An enhanced version of functools.partial() that allows partially applying positional arguments from both the left and right sides of the argument list.

We use ... (the Ellipsis object) as a placeholder in the argument list for the arguments that are not yet supplied. The object returned from partial() also supports being called with ... for further partial application.

Use partial as a function decorator (@partial) to make a function automatically support partial argument application with ....

Usage

As a drop-in replacement for functools.partial():

>>> from partiell import partial
>>> from operator import mul, truediv

>>> double = partial(mul, 2)  # same as functools.partial()
>>> double(5)
10

>>> halve = partial(truediv, ..., 2)  # functools.partial() cannot do this
>>> halve(3)
1.5

As a function decorator to enable smoother partial function application:

>>> @partial
... def f(x, y, z):
...    return x * 100 + y * 10 + z

f() can now be called with ... for partial function application:

>>> g = f(1, ...)  # Supply first argument only (x)
>>> g(2, 3)  # Supply the two remaining arguments (y, z)
123

Functions derived from f() automatically support ... themselves:

>>> h = g(2, ...)  # Supply g's first argument (y)
>>> h(3)  # Supply the final argument (z)
123

Using the ... placeholder also allows supplying arguments right-to-left:

>>> i = f(..., 3)  # Supply last argument only (z)
>>> i(1, 2)  # Supply the remaining arguments (x, y)
123

We can even supply arguments from both ends simultaneously:

>>> j = f(1, ..., 3)  # Supply first and last argument (x, z)
>>> j(2)  # Supply the remaining argument (y)
123

Discussion

Using ... as a placeholder for future function arguments allows for a "functional light" programming style that is somewhere between the verbosity of invoking partial() explicitly and the implicit currying provided by e.g. the @curry decorator in PyMonad.

The idea of using ... as a placeholder for function arguments and having it convert a function call into a partial function application is not new. AFAICS, it was first discussed on python-list in 2005, around the time partial() was first added to the Python standard library (PEP 309).

Installation

Run the following to install:

$ pip install partiell

Development

To work on partiell, clone this repo, and run the following (in a virtualenv) to get everything you need to develop and run tests:

$ pip install -e .[dev]

Alternatively, if you are using Nix, simply use the bundled shell.nix to get a development environment:

$ nix-shell

Use nox to run all tests (as defined in noxfile.py):

$ nox

Contributing

Main development happens at https://github.com/jherland/partiell/. Post issues and PRs there.

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

partiell-0.0.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

partiell-0.0.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file partiell-0.0.2.tar.gz.

File metadata

  • Download URL: partiell-0.0.2.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11

File hashes

Hashes for partiell-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e5422a6494f66a31292674fb5e4ffc25c6466e22092ab19c0f9d90702ee46eda
MD5 767819c4062220975f9c4f32f3ecb736
BLAKE2b-256 9ba4e2d9bc1a81529ca82518c629b983f69f6feae3cfc75c6d9c5c9f348bbbc8

See more details on using hashes here.

File details

Details for the file partiell-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: partiell-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11

File hashes

Hashes for partiell-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49b6508cf9c281e0cc3bfdc0af48621576eae0253d8fbac1c5519b637afdb530
MD5 c7d6fccd9e7eafddd653974847458b52
BLAKE2b-256 7f4c64aeccf0c5578441e89ffee208234ad208f600399a51a4a2e3c004e6f1c5

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