Daily Tech Briefing
AI 科技速览

每天 5 分钟内学习 AI。获取最新的人工智能新闻,理解其重要性,并学习如何将其应用于您的工作。

AI 快讯
Hacker News · 2026/8/2 10:45:11

Show HN: Katharos Functional programming and CSP-style concurrency for Python

AI 中文解读
Katharos让Python写代码像拼乐高,安全又清爽!这个库把函数式编程和并发通信结合起来,处理没有值或出错的情况不再摔异常,而是优雅地递给你一个“结果”。以前写代码要层层检查、处处防备,像在雷区里走路;现在用Maybe和Result这两个工具,代码一条路写到底,遇到坑会自动跳过,像有个贴心助手在导航。出错也不再是炸开的异常,而是打包好的“失败值”,还能保留完整线索让你轻松修bug。对普通用户来说,最直接的感受就是依赖这类库的软件会更稳定,开发者写的代码更安全、逻辑更清晰,以后用的App和网站就会更少莫名其妙崩溃。它让编程不再是高手的黑魔法,普通人也能写出简洁靠谱的代码,技术门槛降低了,好想法落地也就更快了。
Katharos A functional programming and concurrency library for Python. Katharos pairs algebraic abstractions (Functor, Applicative, Monad, Semigroup, Monoid) and concrete types like Maybe, Result, ImmutableList, and IO with message-passing concurrency built on the same functional core. The two halves share one idea: model errors, effects, and concurrent communication as composable, type-safe values. A concurrent hand-off returns a Result, so "the channel closed" is something you handle, not an exception you catch. 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 After: do-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[Maybe, 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(...) Skip the boilerplate with Result.catch: Result.catch turns a function that raises into one that returns a Result, with no manual try/except. Only the declared exception type becomes a Failure; the caught exception keeps its traceback, so you can still find the line that failed. import traceback from katharos.types import Result @Result.catch(ValueError) def parse_int(s: str) -> int: return int(s) parse_int("42") # Success(42) parse_int("??") # Failure(ValueError("invalid literal for int() with base 10: '??'")) failure = parse_int("??") if failure.is_failure(): traceback.print_exception(failure.error) # full traceback, pointing at the failing line 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[Result, int]: x: int = yield parse_positive(5) y: int = yield parse_positive(3) return x + y print(do_block()) # Success(8) Concurrency Katharos provides message-passing concurrency that builds on the same functional core, with room for more than one concurrency model. The first model available is Go-style CSP: launch work concurrently with go (like Go's go f(x)), communicate over typed Channels, and (crucially) rece
分享
阅读原文