Monadic data structures
Project description
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 the length of the body')
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)
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
Close
Hashes for matts-majestic-monads-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dd4445b805f140d9f7c8a7e01f356edb5fa470c6e1ffd4a55ae9a00b5293a62 |
|
MD5 | 1adf71dcc59352ed2e6e25dc65315f23 |
|
BLAKE2b-256 | 7a173c65e3fce28a10e46979994d603f6fc40351a8c0149c0c3f41b0a70a8015 |