Matts Majestic Monads
This overblown library implements Functor, Applicative and Monad in Python.
Synopsis
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
@Either.convert
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 = (
make_parser()
.fmap(parse)
.fmap(to_url)
.bind(read)
)
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
@Function
def parse(parser):
return parser.parse_args()
@Function
def to_url(arguments):
return arguments.URL
both = parse.compose(to_url)
Example
There is a simple example of using this library in example.py.
This script reads a url that you provide and prints the length of the body:
➜ pipenv run python example.py 'http://python.org/'
Right(48817)
➜ pipenv run python example.py 'http://pythonasdasd.org/'
Left(<urlopen error [Errno -2] Name or service not known>)
Metadata
Release files for matts-majestic-monads 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| matts-majestic-monads-0.0.5.tar.gz | 6.5 kB | Without provenance | Details for matts-majestic-monads-0.0.5.tar.gz |
Release files / matts-majestic-monads-0.0.5.tar.gz
| Download URL | matts-majestic-monads-0.0.5.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53b70bbc93cff7aaef6a6091fa444f6418d9b3716f91ee1908e84b83bffee3ca
|
|
BLAKE2b-256 checksum How to use checksums |
bda241ac8812596fe2357d3c683792fb9f6f415129d98378ae7573f55ed33c29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0
|
Security for matts-majestic-monads 0.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.