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.
Project description
simplemonads
Lets make monads easy, fun, and productive.
Platform support
Just pip install simplemonads
and you're done. You can also download this single file 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 file, multiple files in the browser with dynamic loading, and even on microcontrollers with micropython.
Docs
Example GUI using monads: Success
, Failure
, Just
, Reader
, and Printer
import simplemonads as sm
try:
from typing import Callable, Protocol, Union, Any
class Deps(Protocol):
"Dependencies for your application"
def popup(self, msg) -> None:
"Display a popup with the specified message."
import PySimpleGUI
except:
pass
def make(make_gui: "Callable[[],PySimpleGUI]") -> "Callable[[],Deps]":
gui = make_gui()
class GuiDeps:
def popup(self, x: str):
gui.Popup(x)
return GuiDeps
def app(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)
@sm.run
def main():
lib = sm.Success() + (lambda x: __import__("PySimpleGUI"))
gui = lib | {sm.Success: lambda x: x, sm.Failure: lambda x: sm.Printer()}
return app() + make(lambda: gui)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for simplemonads-1.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d634562034f85f6372b2c3a49ebb1789bc432e4fd9207cc6fec808f46bd7ee4e |
|
MD5 | 9117f1aeae1a39b46f20accd0fc4e28b |
|
BLAKE2b-256 | 0bc9dcd1e01cdd60fc714f42e57e7d0fb09e32bb4661ea2a01f3ed80950c48f6 |