Skip to main content

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 logo

Katharos

A functional programming library for Python. Katharos brings algebraic abstractions — Functor, Applicative, Monad, Semigroup, Monoid — together with concrete types like Maybe, Result, ImmutableList, and IO, so you can model effects, errors, and data transformations as composable, type-safe pipelines.

PyPI Docs License Python versions


Installation

pip install katharos

Or using uv

uv add katharos

What it looks like

Before — scattered None checks and exception handling:

user = find_user(user_id)
if user is None:
    return None
account = find_account(user)
if account is None:
    return None
return account.discount

Afterdo-notation that short-circuits cleanly on Nothing:

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)
    account = yield find_account(user)
    return account.discount   # Just(0.15) or Nothing()

Before — nested try/except to propagate errors:

def process(raw: str) -> int:
    try:
        n = parse_int(raw)
    except ValueError as e:
        raise RuntimeError("bad input") from e
    try:
        return validate_positive(n)
    except ValueError as e:
        raise RuntimeError("bad value") from e

After — errors as values, chained with |:

from katharos.types import Result

def process(raw: str) -> Result[Exception, int]:
    return parse_int(raw) | validate_positive   # Failure short-circuits automatically

More examples

Handle optional values without 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()

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").fmap(lambda n: n * 2) # Success(84)
parse_int("??").fmap(lambda n: n * 2)  # Failure(...)

Combine values with the Semigroup operator:

from katharos.types import ImmutableList

ImmutableList([1, 2]) @ ImmutableList([3, 4])  # ImmutableList([1, 2, 3, 4])

Do-notation

do-notation works with any monad — Maybe, Result, IO, ImmutableList and your custom monads. Each yield unwraps the monadic value:

from katharos.syntax_sugar import do, DoBlock
from katharos.types import Result

def parse_positive(x: int) -> Result[ValueError, int]:
    return Result.Success(x) if x > 0 else Result.Failure(ValueError(f"{x} is not positive"))

# Clean, imperative-style monadic code
@do(Result)
def do_block() -> DoBlock[int]:
    x: int = yield parse_positive(5)
    y: int = yield parse_positive(3)
    return x + y

print(do_block())  # Success(8)

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

katharos-1.0.1.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

katharos-1.0.1-py3-none-any.whl (32.5 kB view details)

Uploaded Python 3

File details

Details for the file katharos-1.0.1.tar.gz.

File metadata

  • Download URL: katharos-1.0.1.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for katharos-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ae7800e19fa27f90c2e447f972928f8a97d9359abc7b51cf7bd6a79f3e00e54b
MD5 85a55a213efe887936a75fdf1bcc6a48
BLAKE2b-256 b1d1888733a6b951b73b6319999b0c21f72b5cd4ad50cd5f60487fbef3cc2184

See more details on using hashes here.

File details

Details for the file katharos-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: katharos-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 32.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for katharos-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 300931655c824d75020acf979064d5ba6c1e850bc1d572dabea0c54d30f97277
MD5 9401651518946030131678d4d9144a27
BLAKE2b-256 d8000dfb9c32251e3eda9e9e88e2a90ae05f9eb3513fd8335f18eeed76a64111

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page