Skip to main content

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

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

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simplemonads-1.0.12.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

simplemonads-1.0.12-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file simplemonads-1.0.12.tar.gz.

File metadata

  • Download URL: simplemonads-1.0.12.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure

File hashes

Hashes for simplemonads-1.0.12.tar.gz
Algorithm Hash digest
SHA256 8771f05dcd05e8bc9715c61d073de2d69f380ec000074cbf628aee637c533cba
MD5 5fee65985a1aad0fd00a330417c54513
BLAKE2b-256 d0bceb65f2bdb3d9f3bd995aeb68420707ee971fceccd03b07886d7bde90e18e

See more details on using hashes here.

File details

Details for the file simplemonads-1.0.12-py3-none-any.whl.

File metadata

  • Download URL: simplemonads-1.0.12-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure

File hashes

Hashes for simplemonads-1.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 dd81c5f2f6a5bc920eca02ef65a7e9a2d70153399fbf8a0a1bcfce3b0d2eb6cf
MD5 efc014cc3c2fbe8ff80e090c74f4581b
BLAKE2b-256 ec6f14075b15d707d7aec69622a7a96d791ceb7f0af539820236ee8811e50e8c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page