Skip to main content

Minimalist Currying implementation for Python

Project description

kare

Minimal implementation of Function Currying for python.

Usage

You can curry any callable by applying the curry function to it:

from kare import curry

def my_sum(x: int, y: int, z: int) -> int:
    return x + y + z

curried_sum = curry(my_sum)

Curried functions take a single argument and return either a new function that takes a single argument or the result of applying all the arguments passed so far to the original function:

sum_two = curried_sum(2)
sum_five = sum_two(3)
sum_five(1) # == 6, equivalent to my_sum(2, 3, 1)

If you chain multiple calls together for a more succint notation:

sum_five = curried_sum(2)(3)

The curry function also works as a decorator:

@curry
def my_curried_sum(x: int, y: int, z: int) -> int:
    return x + y + z

add_six = my_curried_sum(2)(4)

Currently we only support functions with positional and specified number of arguments. The following:

@curry  # This wil raise an exception
def variadic_positional_function(*args):
    ...

@curry # This wil raise an exception
def variadic_positional_function(*, x: int, y: int):
    ...

@curry # This wil raise an exception
def variadic_positional_function(x: int, y: int, **kwargs):
    ...

If you need to do partial application on keyword arguments you can use functools' partial as usual.

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

kare-0.0.1.tar.gz (2.7 kB view hashes)

Uploaded Source

Built Distribution

kare-0.0.1-py3-none-any.whl (2.8 kB view hashes)

Uploaded Python 3

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