Skip to main content

Monadic data structures

Project description

Matts Majestic Monads

pipeline status coverage report

This overblown library implements Functor, Applicative and Monad in Python.


You can use Either.convert or Maybe.convert as a decorator to convert return types. This example converts exceptions into Left values:

from monads import Either

def parse_arguments():
    parser = argparse.ArgumentParser(description='Download a URL and print it')
    parser.add_argument('URL', help='URL to download')
    return parser.parse_args()

Once you have your Either you can use fmap and bind to operate over the contained value:

result = (

The methods will be passed the current value. You should use fmap when the function will return a normal value. When the function returns a monad, use bind.

You can use Function as a decorator to convert functions. Once converted they become composable:

from monads import Function

def parse(parser):
    return parser.parse_args()

def to_url(arguments):
    return arguments.URL

both = parse.compose(to_url)


There is a simple example of using this library in This script reads a url that you provide and prints the length of the body:

➜ pipenv run python ''
➜ pipenv run python ''
Left(<urlopen error [Errno -2] Name or service not known>)

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

matts-majestic-monads-0.0.5.tar.gz (6.5 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page