Python’s imperative and object oriented nature does not always make it easy to adopt a functional programming mindset.
Sure, it does have map
and filter
as built-in functions, but it’s hardly enough, and relies on keeping intermediate variables to chain easily.
This simple library aims at providing simple tools allowing to chain transformations on values and maintaining readability at the core of your code.
The main tool of this package is the Pipeline object. This objects is meant to be ephemeral and used to chain operations on another value.
from python_funky.Pipeline import Pipeline
intermediates = []
result = Pipeline([1, 2, 3]) \
.map(lambda x: x * x) \
.filter(lambda x: x % 2 != 0) \
.each(lambda x: intermediates.append(x)) \
.reduce(0, lambda x, acc: x + acc) \
.then(lambda x: range(0, x)) \
.then(sum) \
.get()
assert result == 45
assert intermediates == [1, 9]