Skip to content

A Simple Tiny One-shot Algebraic Effects Library for Python.

Notifications You must be signed in to change notification settings

Catminusminus/simple-eff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Eff

A Simple Tiny (only <100 lines) Algebraic Effects Library for Python.

from typing import Any, Callable, Generator, Optional

from simple_eff import Effect, eff, Eff

not_integer = Effect()


def parse_int(str: str) -> Optional[int]:
    try:
        return int(str)
    except Exception:
        return None


@eff
def sum_lines(s: str) -> Generator[Eff[str], int, int]:
    lines = s.split()
    sum = 0
    for line in lines:
        match parse_int(line):
            case None:
                sum += yield not_integer.perform(line)
            case num:
                sum += num
    return sum


def handle_notinteger(k: Callable, v: str) -> Any:
    print(f"Parse Error: {v} is not an integer.")
    return k(0)


if __name__ == "__main__":
    twelve = sum_lines("1\n2\nthree\n4\n5")
    twelve.on(not_integer, handle_notinteger)
    ret = twelve.run()
    print(f"Sum: {ret}")

Acknowledgement

This library is ...

  • inspired by effective-rust for the API design.
  • heavily based on ruff for the implementation.

About

A Simple Tiny One-shot Algebraic Effects Library for Python.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages