Skip to main content

Function composition in python

Project description

Composable functions

F#-style function composition for Python. Compose functions using bitshift operators >> & <<

Installation

TBD

Usage

You can wrap any Callable with composable to make it a composable function. Composable functions can be composed with other Callable objects using the bit shift operators (<< & >>):

from composable.functions import composable as c

def add_one(x: int) -> int:
    return x + 1

def add_two(x: int) -> int:
    return x + 2

c_add_one = c(add_one)
c_add_two = c(add_two)

# You can compose with other composables:
add_three = c_add_one >> c_add_two
# Equivalent to:
# add_three = lambda x: add_two(add_one(x))
add_three(5)  # == 8

# Or with any `Callable` object
add_five = c_add_one >> add_two >> add_two
# Equivalent to:
# add_five = lambda x: add_two(add_two(add_one(x)))
add_five(5)  # == 10

It also works as a decorator:

from composable.functions import composable

@composable
def add_one(x: int) -> int:
    return x + 1

add_three = add_one >> add_one >> add_one

Complex pipelines can be built by reusing simple functions:

from composable.functions import compose
import io

fake_stream
word_counter = (
    I >> str.strip
    >> str.split
    >> len
)
word_counter(line) == 6

You can also compose multiple functions at once with compose:

This can be useful to programatically build complex functions

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

composable-functions-0.0.0.tar.gz (2.6 kB view hashes)

Uploaded Source

Built Distribution

composable_functions-0.0.0-py3-none-any.whl (2.7 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