https://github.com/sdaves/simplemonads
Easy to use monads (containers), with pattern matching, that improve the quality of your python code. Use Just to end checking for None, Success to end unhandled exceptions, Future for async, and Reader for dependencies.
https://github.com/sdaves/simplemonads
dependency-injection fp functional-programming futures monads python reader unhandled-exceptions
Last synced: 7 months ago
JSON representation
Easy to use monads (containers), with pattern matching, that improve the quality of your python code. Use Just to end checking for None, Success to end unhandled exceptions, Future for async, and Reader for dependencies.
- Host: GitHub
- URL: https://github.com/sdaves/simplemonads
- Owner: sdaves
- License: unlicense
- Created: 2020-10-13T21:18:01.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-10-28T07:48:16.000Z (over 5 years ago)
- Last Synced: 2025-07-23T04:43:33.206Z (8 months ago)
- Topics: dependency-injection, fp, functional-programming, futures, monads, python, reader, unhandled-exceptions
- Language: HTML
- Homepage:
- Size: 854 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [simplemonads](https://sdaves.github.io/simplemonads/)
Let's make monads easy, fun, and productive.
## Platform support
Just `pip install simplemonads` and you're done. You can also download [this single file](https://sdaves.github.io/simplemonads/tests/simplemonads.py) into your project and use it as you wish without dependencies. Works across all platforms, so CPython >= 3.5 (Windows, Linux, Mac, Android, iOS), in a [single standalone html](https://sdaves.github.io/simplemonads/tests/test_brython_standalone.html) file, multiple files in the browser with [dynamic loading](https://sdaves.github.io/simplemonads/tests/index.html), and [even on microcontrollers with micropython](https://micropython.org).
## Docs
[Read the docs here.](https://sdaves.github.io/simplemonads/docs/)
## Example GUI using monads: `Success`, `Failure`, `Just`, `Reader`, and `Printer`

```python
import simplemonads as sm
try:
class Deps(sm.Protocol):
"Dependencies for your application"
def popup(self, msg: str) -> None:
"Display a popup with the specified message."
except:
pass
@sm.run
class TestReader:
@classmethod
def make(cls, create: "sm.Callable[[],sm.Any]") -> "sm.Callable[[],Deps]":
gui = create()
class GuiDeps:
def popup(self, x: str):
gui.Popup(x)
return GuiDeps
@classmethod
def app(cls, divide_by_zero: bool = False) -> sm.Reader:
data = sm.Success(sm.Just(7))
double = lambda x: x + (lambda y: y * 2)
triple = lambda x: x + (lambda y: y * 3)
result = data + triple + double
if divide_by_zero:
result += lambda x: x + (lambda x: x / 0)
def effect(deps: "Deps") -> "sm.Monad":
msg = "Answer to the Universe: "
err = "Whoops, an error happened: "
result | {
sm.Success: lambda x: x
| {sm.Just: lambda val: deps.popup(msg + str(val))},
sm.Failure: lambda x: deps.popup(err + x),
}
return result
return sm.Reader(effect)
@classmethod
def main(cls):
gui = sm.Success() + (lambda x: __import__("PySimpleGUI")) | {
sm.Success: lambda x: x,
sm.Failure: lambda x: sm.Printer(),
}
return cls.app() + cls.make(lambda: gui)
```