Skip to main content

A simple tiny algebraic effects library for Python

Project description

Simple Eff

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

from simple_eff import Effect, eff

not_integer = Effect()

def parse_int(str):
    try:
        return int(str)
    except:
        return None


@eff
def sum_lines(s: str) -> 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, v):
    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.

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

simple-eff-0.0.1a2.tar.gz (2.2 kB view hashes)

Uploaded Source

Built Distribution

simple_eff-0.0.1a2-py3-none-any.whl (2.2 kB view hashes)

Uploaded Python 3

Supported by

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