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)
add_three = c_add_one >> c_add_two
add_three(5)
add_five = c_add_one >> add_two >> add_two
add_five(5)
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