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 details)

Uploaded Source

Built Distribution

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

composable_functions-0.0.0-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file composable-functions-0.0.0.tar.gz.

File metadata

  • Download URL: composable-functions-0.0.0.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.12 Darwin/20.4.0

File hashes

Hashes for composable-functions-0.0.0.tar.gz
Algorithm Hash digest
SHA256 c6cc0ac6c88d6978f5bf4758d49f64468c5cf1f867da9addcd6313777a2afe48
MD5 1eae4eeb71db93d502ebb02f16986e13
BLAKE2b-256 d2f85a9b23c88fb967b4d3378913d010ce39f7edb59715dabb5047a327b84f4a

See more details on using hashes here.

File details

Details for the file composable_functions-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for composable_functions-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6de5842d33674f476d42202d190fb7ca134b86b8222878b9c93473d2e83e7141
MD5 f22fae62fec1d4c8a7e67c8469244b00
BLAKE2b-256 3c2cdbbb9236a9eb3365d233182682c55f4403c8b9b6a85b9bee47bfea0ba653

See more details on using hashes here.

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