Functional programming tools for Python
Project description
pyferret
This Python library provides functional programming tools like the "maybe" and "result" monads. These monads help handle optional values and error handling in a functional way. The "maybe" monad deals with null or undefined values, while the "result" monad manages computations that may fail and return an error. These tools promote clean, reliable, and concise code by promoting immutability, separation of concerns, and composability.
Not pretending on full correspondence with theoretical part of related instruments, because of not all of them can be implemented in Python context with comfortable usage. There may be implemented not all details that required by some abstractions in FP or provided some additional stuff for usability.
- pyferret
Installation
pip install pyferret
Function composition
In Python function composition may be quite nice and useful tool. Function composition is a technique in functional programming where multiple functions are combined together to create a new function. The output of one function becomes the input of the next function, forming a chain of transformations. This allows for the creation of complex and reusable logic by breaking it down into smaller, composable parts.
To use a composition that corresponds this description in Python we need some helpful tools which this library provides.
We have some calculating functions:
def compute_x() -> int | None: ...
def compute_y() -> int | None: ...
def calculate_coef(x: int) -> int: ...
def format_result(x: str) -> str: ...
def dispatch(x: str) -> str | None: ...
def process_x() -> str | None:
computed_x = compute_x()
if computed_x:
coef = calculate_coef(computed_x)
formatted = format_result(str(coef).upper())
return dispatch(formatted)
That simple function take too much visual burden. Despite we declared our function with word "process" it's not look like a actual process. We have one simple condition - if some of processing steps is None then we return None, and every that step requires an if block to handle.
How that can look like with function composition.
from pyferret.maybe import Maybe
def compute_x() -> Maybe[int]: ...
def compute_y() -> Maybe[int]: ...
def calculate_coef(x: int) -> int: ...
def format_result(x: str) -> str: ...
def dispatch(x: str) -> Maybe[str]: ...
# Result type is `Just[str] | Nothing`
def process_x() -> Maybe[str]:
return (
compute_x()
.fmap(calculate_coef)
.fmap(str)
.fmap(format_result)
.fmap(str.upper)
.bind(dispatch)
)
# `process_x` is also can be composed in next calls
# `result` type is `Just[int] | Nothing`
result = process_x().fmap(str.lower).fmap(str.split).fmap(len)
Meaning of Context
In functional programming, a context refers to additional information or state associated with a computation or value.
In here context is simple container that stores a single value, but have meaning for us, like a list or tuple with one element. For example we can have 500 as computation result and 500 as error code, same value and different result.
ERROR_CODE = 500
def compute_or_error_code() -> int:
try:
_ = 500 / randint(0, 1)
except ZeroDivisionError:
return ERROR_CODE
result = compute_or_error_code()
# In this case we have probability that `result` can be an error and
# a computation result
is_error = result == ERROR_CODE # This may be not an error
Value of Functor
Functor defined here as Context which have fmap method.
Let's say we have a Functor named A with value 100 (we say that Functor is a Context, while Context is just a container for a value). Then fmap simply takes a function that can do operation on type of A and packs result of function to same Context.
result = SomeFunctor(100).fmap(str) # SomeFunctor("100")
Importance of Monad
Monad defined here as Functor which have bind method.
Let's say we have a Monad named A with value 100, and function that takes parameter type of A and return same Monad B with different or same type of context value. So bind taking this function, provide operation on value of A, and return B.
op = lambda x: SomeMonad("Yes!")
result = SomeMonad(100).bind(op) # SomeMonad("Yes!")
Maybe
A "maybe" monad handles computations that may or may not produce a value. It represents uncertainty or potential failure. The value can be "Just x" or "Nothing". This allows for chaining computations and concise error handling.
Let's dive into example:
def compute_or_none() -> int | None: ...
# Result is the int or None, but None can have meaning in some context,
# None is the object which represents void, but it's not a void, it's value
result = compute_or_none()
ERROR_CODE = 500
def side_effect() -> None | Literal[ERROR_CODE]: ...
result = side_effect()
# In that context None represents success of operation, and value represent
# an error
if result == ERROR_CODE: ...
A Maybe monad can help to qualify what exactly function will return in that case.
from pyferret.maybe import Maybe, Just, Nothing
def compute_or_none() -> Maybe[int]: ...
result = compute_or_none()
# We can check is value present in result by accessing some properties
if result.is_some: ...
if result.is_nothing: ...
ERROR_CODE = 500
# Same as previous but in manner on `Maybe` usage
def side_effect() -> Maybe[ERROR_CODE]: ...
result = side_effect()
if result.is_some: ...
How Maybe can help with function composition?
Maybe can be treated as Just[Any] | Nothing, where Just and Nothing is a Context that stores a value, but Nothing not providing access to inner value because meaning of this word say that there's can't be a value.
We defined that Maybe is a monad, then it have fmap, bind and other helpful methods.
Maybe API
Initialize instance
>>> some = Just(1)
>>> nothing = Nothing()
isintance checks
>>> isinstance(some, Just)
True
>>> isinstance(nothing, Nothing)
True
Unsafe accessing the value
>>> some.value
1
>>> nothing.value
ValueError: Attempt to get value on Nothing
Safe accessing the value
>>> some.get_value_or("default")
1
>>> nothing.get_value_or("default")
'default'
Boolean checks
>>> some.is_some
True
>>> nothing.is_some
False
Mapping functions
Basic fmap:
>>> some.fmap(lambda x: x * 3 * 10)
Just 30
>>> nothing.fmap(lambda x: x * 3 * 10)
Nothing
We may need make side effect with value inside Just, but preserve this value and ignore function return:
>>> some.fmap_through(lambda x: print(x))
1 # print(x)
Just 1 # print returns `None`, but some.value is preserved in context
>>> nothing.fmap_through(lambda x: print(x))
Nothing
Partial application mapped function:
>>> some.fmap_partial(lambda x, y: x * y, y=25)
Just 25
>>> nothing.fmap_partial(lambda x, y: x * y, y=25)
Nothing
Partial application with preserving inner value, fmap_through and fmap_partial combined:
>>> some.fmap_partial_through(lambda x, y: print(x + y), y=25)
26 # print(x + y)
Just 1
>>> nothing.fmap_partial_through(lambda x, y: print(x + y), y=25)
Nothing
Binding functions
Basic bind:
>>> some.bind(lambda x: Just(x + 10))
Just 11
>>> nothing.bind(lambda x: Just(x + 10))
Nothing
>>> some.bind(lambda x: Nothing())
Nothing
>>> nothing.bind(lambda x: Nothing())
Nothing
Binding and preserving inner value:
>>> def side_effect(x: int) -> Maybe[str]:
... print(x)
... return Just("Ok")
...
>>> some.bind_through(side_effect)
1
Just 1
>>> nothing.bind_through(side_effect)
Nothing
Partial application binding function:
>>> some.bind_partial(lambda x,y: Just(x + y), y=34)
Just 35
>>> nothing.bind_partial(lambda x,y: Just(x + y), y=34)
Nothing
Partial application and preserving inner value:
>>> def side_effect(x: int, y: int) -> Maybe[str]:
... print(x + y)
... return Just("Ok")
...
>>> some.bind_partial_through(side_effect, y=42)
43
Just 1
>>> nothing.bind_partial_through(side_effect, y=42)
Nothing
Result
The Result Monad is a way to encapsulate the outcome of a computation in a type that can represent either a successful result or an error.
A little example:
from pyferret.result import Result, Ok, Err
def compute_or_error() -> Result[int, int]: ...
result = compute_or_error()
# We can check success of operation by accessing some properties
if result.is_ok: ...
if result.is_err: ...
ERROR_CODE = 500
# If side-effect may fail
def side_effect() -> Result[None, ERROR_CODE]: ...
result = side_effect()
if result.is_ok: ...
How Result can help with function composition?
Result can be treated as Ok[Any] | Err[Any], where both Ok and Err is a Context that stores a value, but with semantic about operation result.
Result also have all Monad functions - fmap, bind.
Result API
Initialize instance
>>> ok = Ok(1)
>>> err = Err("error")
isintance checks
>>> isinstance(ok, Ok)
True
>>> isinstance(err, Err)
True
Unsafe accessing the value
>>> ok.ok_value
1
>>> ok.err_value
ValueError: Attempt to get err value on Ok: 1
>>> err.err_value
'error'
>>> err.ok_value
ValueError: Attempt to get ok value on Err: error
Safe accessing the value
>>> ok.get_ok_or("default")
1
>>> ok.get_err_or("default")
'default'
>>> err.get_ok_or("default")
'default'
>>> err.get_err_or("default")
'error'
Boolean checks
>>> ok.is_ok
True
>>> ok.is_err
False
>>> err.is_ok
False
>>> err.is_err
True
Mapping functions
Basic fmap:
>>> ok.fmap(lambda x: x * 3 * 10)
Ok 30
>>> err.fmap(lambda x: x * 3 * 10)
Err error
We may need make side effect with value inside Ok, but preserve this value and ignore function return:
>>> ok.fmap_through(lambda x: print(x))
1 # print(x)
Ok 1
>>> err.fmap_through(lambda x: print(x))
Err 'error'
Partial application mapped function:
>>> ok.fmap_partial(lambda x, y: x * y, y=25)
Ok 25
>>> err.fmap_partial(lambda x, y: x * y, y=25)
Err 'error'
Partial application with preserving inner value, fmap_through and fmap_partial combined:
>>> ok.fmap_partial_through(lambda x, y: print(x + y), y=25)
26 # print(x + y)
Ok 1
>>> err.fmap_partial_through(lambda x, y: print(x + y), y=25)
Err 'error'
Binding functions
Basic bind:
>>> ok.bind(lambda x: Ok(x + 10))
Ok 11
>>> err.bind(lambda x: Ok(x + 10))
Err 'error'
>>> ok.bind(lambda x: Err("another error"))
Err 'another error'
>>> err.bind(lambda x: Err("another error"))
Err 'error'
Binding and preserving inner value only in case of Ok:
>>> def side_effect(x: int) -> Result[str, str]:
... print(x)
... return Ok("Ok")
...
>>> ok.bind_through(side_effect)
1
Ok 1
>>> err.bind_through(side_effect)
Err 'error'
# In that case Err will be taken from `side_effect_error` function
>>> def side_effect_error(x: int) -> Result[str, str]:
... print(x)
... return Err("side effect error")
>>> ok.bind_through(side_effect_error)
1
Err 'side effect error'
>>> err.bind_through(side_effect_error)
Err 'error'
Partial application binding function:
>>> ok.bind_partial(lambda x,y: Ok(x + y), y=34)
Ok 35
>>> ok.bind_partial(lambda x,y: Err(x + y), y=34)
Err 35
>>> err.bind_partial(lambda x,y: Ok(x + y), y=34)
Err 'error'
>>> err.bind_partial(lambda x,y: Err(x + y), y=34)
Err 'error'
Partial application and preserving inner value only if result is Ok:
>>> def side_effect(x: int, y: int) -> Result[str, int]:
... print(x + y)
... return Ok("Ok")
...
>>> ok.bind_partial_through(side_effect, y=25)
26
Ok 1
>>> err.bind_partial_through(side_effect, y=25)
Err 'error'
>>> ok.bind_partial_through(side_effect_error, y=25)
26
Err 'side effect error'
>>> err.bind_partial_through(side_effect_error, y=25)
Err 'error'
Helpers
Pyfferet provides a set of convenient helper functions to simplify and assist with common tasks.
Maybe from optional
>>> from pyferret.helpers import from_optional
>>> a: int | None = 12
>>> from_optional(a)
Just 12
>>> a: int | None = None
>>> from_optional(a)
Nothing
List concatenation
>>> concat([[1,2,3], [4,5,6], [7,8,9]])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
TODO
-
Maybemethods that returns value out of contexts -
Resultmethods that returns value out of contexts - Complete docs
-
Resultmethods for working with exceptions and traceback -
Resultmethods for fmap and bindErrcontext - Helper functions
- From optional for
Maybe - Concat list
- ...
- From optional for
-
Maybetest coverage -
Resulttest coverage -
abstracttest coverage -
pypipublish and versioning - GitGub Actions
- typecheck
- codestyle
- Hash and compare methods
- Better
*argsand**kwargstyping
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyferret-0.2.1.tar.gz.
File metadata
- Download URL: pyferret-0.2.1.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77aa18d11afba2109e8359aac4377e979c146a55b13ae754b1061bd8498565af
|
|
| MD5 |
86670d102e9930cc489f1bf60b8b2b64
|
|
| BLAKE2b-256 |
3576ef0705546acef8ad26170f559ca9c205cad3488c2059ff14f7db771fd50a
|
File details
Details for the file pyferret-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pyferret-0.2.1-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
166e3af9bc6a2b9d6b6d152086672f67b83c9a407c792a437dcc1a44229d0c0c
|
|
| MD5 |
2fcb05cbd23ed5fd8e8f5298027f0aee
|
|
| BLAKE2b-256 |
085f4907b502ba768330bb38ba9ed5b45e61cf08ac35cda564f97b6eef067e5a
|