Skip to content

Repository files navigation

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 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

Read the docs here.

Example GUI using monads: Success, Failure, Just, Reader, and Printer

Screenshot of test_reader.py

importsimplemonadsassmtry:
classDeps(sm.Protocol):
"Dependencies for your application"defpopup(self, msg: str) ->None:
"Display a popup with the specified message."except:
pass@sm.runclassTestReader:
@classmethoddefmake(cls, create: "sm.Callable[[],sm.Any]") ->"sm.Callable[[],Deps]":
gui=create()
classGuiDeps:
defpopup(self, x: str):
gui.Popup(x)
returnGuiDeps@classmethoddefapp(cls, divide_by_zero: bool=False) ->sm.Reader:
data=sm.Success(sm.Just(7))
double=lambdax: x+ (lambday: y*2)
triple=lambdax: x+ (lambday: y*3)
result=data+triple+doubleifdivide_by_zero:
result+=lambdax: x+ (lambdax: x/0)
defeffect(deps: "Deps") ->"sm.Monad":
msg="Answer to the Universe: "err="Whoops, an error happened: "result| {
sm.Success: lambdax: x| {sm.Just: lambdaval: deps.popup(msg+str(val))},
sm.Failure: lambdax: deps.popup(err+x),
}
returnresultreturnsm.Reader(effect)
@classmethoddefmain(cls):
gui=sm.Success() + (lambdax: __import__("PySimpleGUI")) | {
sm.Success: lambdax: x,
sm.Failure: lambdax: sm.Printer(),
}
returncls.app() +cls.make(lambda: gui)

About

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.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages