Easy to use monads (containers) 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
Easy to use monads (containers) 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.
Platform support
Just pip install simplemonads
and you're done. You can also copy the single file simplemonads/__init__.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 browser with Brython, and even on microcontrollers with micropython!
Example using monads: Success
, Failure
, Just
, Reader
, and Printer
from simplemonads import Success, Failure, Just, _, Reader, run, Printer
class AppDeps:
def __init__(self, gui=Printer()):
self.gui = gui
def app(divide_by_zero=False):
data = Success(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: AppDeps):
return result | {
Success:lambda x: x | {
Just:lambda val: deps.gui.Popup('Answer to the Universe: ' + str(val))
},
Failure:lambda x: deps.gui.Popup('Whoops, an error happened: ' + x)
} is result or result
return Reader(effect)
class GuiAppDeps(AppDeps):
def __init__(self, gui=Printer()):
try:
import PySimpleGUI
self.gui = PySimpleGUI
except:
self.gui = gui
@run
def main():
return app() + GuiAppDeps
Handling exceptions
To demonstrate exception handling the above example can be changed to:
return app(True) + GuiAppDeps
This will result in safely handling the divide by zero exception and will run the following without interrupting the flow of the application:
Failure:lambda x: deps.gui.Popup('Whoops, an error happened: ' + x)
Example monad Future
from simplemonads import Future
async def effect(data=1):
import asyncio
await asyncio.sleep(0.1)
return data ** data
assert Future(2) | { Future: lambda x: x } == 2
assert Future(2) + effect | { Future: lambda x: x } == 4
assert Future(2) + effect + effect | { Future: lambda x: x } == 256
This allows easily adding async functions into the monadic pipeline. If you need special concurrency options, you can await multiple tasks from inside the effect function.
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.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ebe601d8fbaf1eb7e1bef12c7d7f2053c10c71b9c057afb8ce90b06c1256535 |
|
MD5 | 750ef18ccd4b9f30f8c937723cb5a58b |
|
BLAKE2b-256 | 25de559a480da4004f4ac42e474257db6679b99036821a770790dd9de997fe2a |