Introducing: the From
class.
For maintainable data pipelines.
- Adds new syntactic sugar to python!
- Cleanly abstract side-effects without interrupting control flow
- Elegant decorators included
- Decorate any function with
@to(From)
to make it monadic - Explicit function composition
- Currently implemented: Maybe
- Coming soon: Result, IO
pip install -e encapsulation
Get started by
from encapsulation.base import Maybe
Maybe("a") + "b" # outputs <Maybe val=(ab)>
Maybe(None) * 2 # outputs <Nothing val=(None)>
Easy to use decorators that elevate functions to return wrapped values instead:
from encapsulation.base import Maybe, to
def add1(n: int):
return n + 1
@to(Maybe)
def test(s: int):
return s
test(1) << add1 & print << add1 # prints '2', while returning Maybe(3)
You can chain computations using compose(). For example
from encapsulation.base import Just, compose
@to(Just[int])
def add2(s: int):
return s + 2
compose(Just(1), add2, add2) # equals Just(5)