A functional programming library for Python that provides algebraic abstractions like Semigroups, Monoids, Functors, Applicatives, and Monads, along with immutable data structures to enable composable, type-safe, and side-effect-free code.
Project description
Katharos
Functional programming abstractions for Python — Monads, Applicatives, Functors, and immutable data structures for composable, type-safe code.
Installation
pip install katharos
Quick Start
Handle optional values without None checks:
from katharos.types import Maybe
result = Maybe.Just(5) | (lambda x: Maybe.Just(x * 2)) # Just(10)
nothing = Maybe.Nothing() | (lambda x: Maybe.Just(x * 2)) # Nothing()
Model errors as values instead of exceptions:
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.Success(n * 2)) # Success(84)
parse_int("??") | (lambda n: Result.Success(n * 2)) # Failure(...)
Chain operations with do-notation:
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()
Combine values with the Semigroup operator:
from katharos.types import ImmutableList
ImmutableList([1, 2]) @ ImmutableList([3, 4]) # ImmutableList([1, 2, 3, 4])
Documentation
Full tutorials, how-to guides, API reference, and explanations of the mathematical foundations are at katharos.readthedocs.io.
License
MIT
Project details
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 katharos-1.0.0.tar.gz.
File metadata
- Download URL: katharos-1.0.0.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0a297f5614dbd92649d6a7d1f7ac2e0dd18b9fb39240601552e0c6f792e2e32
|
|
| MD5 |
d46e641cb2223398c3073ebd020399d1
|
|
| BLAKE2b-256 |
7c5f3a29068f60b825d681dffd6bced51d71a5ac2c9e4ead6a69062ea95c9838
|
File details
Details for the file katharos-1.0.0-py3-none-any.whl.
File metadata
- Download URL: katharos-1.0.0-py3-none-any.whl
- Upload date:
- Size: 31.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
872b650d4655f5e2db8bee62ddcc2080f00cd11c464b201f5b11ffe9bc0330d5
|
|
| MD5 |
eac6717fa26da201ff3b3d85a2a11f04
|
|
| BLAKE2b-256 |
16a250ade894553abfab37aa0d26e6b5cedda21f48a0da19d7ab9216f8fe3a1d
|