Project details
Katharos is a Python library that enhances functional programming with powerful abstractions. It features Monads, Applicatives, and Functors, alongside immutable data structures, enabling developers to write composable and type-safe code. Streamline your programming experience with elegant solutions for handling optional values and modeling errors.
Katharos is a powerful library designed for functional programming in Python, offering essential abstractions such as Monads, Applicatives, Functors, and immutable data structures. This enables developers to write composable and type-safe code with ease.
None checks:from katharos.types import Maybe
result = Maybe[int].Just(5) | (lambda x: Maybe[int].Just(x * 2)) # Just(10)
nothing = Maybe[int].Nothing() | (lambda x: Maybe[int].Just(x * 2)) # Nothing()
from katharos.types import Result
def parse_int(s: str) -> Result[ValueError, int]:
try:
return Result.Success(int(s))
except ValueError as e:
return Result.Failure(e)
parse_int("42") | (lambda n: Result[ValueError, int].Success(n * 2)) # Success(84)
parse_int("??") | (lambda n: Result[ValueError, int].Success(n * 2)) # Failure(...)
from katharos.types import Maybe
from katharos.syntax_sugar import do, DoBlock
@do(Maybe)
def lookup_discount(user_id: int) -> DoBlock[float]:
user = yield find_user(user_id) # short-circuits on Nothing
account = yield find_account(user)
return account.discount
lookup_discount(42) # Just(0.15) or Nothing()
from katharos.types import ImmutableList
ImmutableList([1, 2]) @ ImmutableList([3, 4]) # ImmutableList([1, 2, 3, 4])
Comprehensive tutorials, guides, API references, and the underlying mathematical foundations are available at katharos.readthedocs.io.
Comments
0Start the conversation
Share the first comment.