https://github.com/suned/python-algebraic-effects
Purely Functional, coroutine based algebraic effects in Python
https://github.com/suned/python-algebraic-effects
Last synced: about 1 month ago
JSON representation
Purely Functional, coroutine based algebraic effects in Python
- Host: GitHub
- URL: https://github.com/suned/python-algebraic-effects
- Owner: suned
- Created: 2021-11-05T19:26:37.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-11-05T20:07:26.000Z (over 4 years ago)
- Last Synced: 2025-03-03T02:13:20.864Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# python-algebraic-effects
Purely Functional, coroutine based algebraic effects in Python
# Example:
```python
from typing import Generator, Union, Any, Dict, Type
from ask import Ask, AskHandler
from files import ReadFile, ReadFileHandler
from console import Print, PrintHandler
from effect import run, Handler
def f() -> Generator[Union[Ask, ReadFile], Any, str]:
name: str = yield Ask()
content: str = yield ReadFile(name)
return content
def test() -> Generator[Union[Ask, ReadFile, Print], Any, str]:
content: str = yield from f()
yield Print(content)
return 'done'
handlers: Dict[Type, Handler] = {
ReadFile : ReadFileHandler(),
Ask : AskHandler('effect.py'),
Print : PrintHandler()
}
run(test(), handlers)
```