monad - a functional library
Project description
Introduction
What?
Monads in python, with some helpful functions.
How?
>>> from monad.decorators import maybe
>>> parse_int = maybe(int)
>>> parse_int(42)
Just(42)
>>> parse_int('42')
Just(42)
>>> parse_int('42.2')
Nothing
>>> parse_float = maybe(float)
>>> parse_float('42.2')
Just(42.2)
>>> from monad.actions import tryout
>>> parse_number = tryout(parse_int, parse_float)
>>> tokens = [2, '0', '4', 'eight', '10.0']
>>> [parse_number(token) for token in tokens]
[Just(2), Just(0), Just(4), Nothing, Just(10.0)]
>>> @maybe
... def reciprocal(n):
... return 1. / n
>>> reciprocal(2)
Just(0.5)
>>> reciprocal(0)
Nothing
>>> process = parse_number >> reciprocal
>>> process('4')
Just(0.25)
>>> process('0')
Nothing
>>> [process(token) for token in tokens]
[Just(0.5), Nothing, Just(0.25), Nothing, Just(0.1)]
>>> [parse_number(token) >> reciprocal for token in tokens]
[Just(0.5), Nothing, Just(0.25), Nothing, Just(0.1)]
>>> [parse_number(token) >> reciprocal >> reciprocal for token in tokens]
[Just(2.0), Nothing, Just(4.0), Nothing, Just(10.0)]
Why?
Why not.
Requirements
CPython >= 2.7
Installation
Install from PyPI:
pip install monad
Install from source, download source package, decompress, then cd into source directory, run:
make install
License
BSD New, see LICENSE for details.
Links
- Documentation:
- Issue Tracker:
- Source Package @ PyPI:
- Mercurial Repository @ bitbucket:
- Git Repository @ Github:
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
monad-0.1.tar.gz
(24.1 kB
view details)
File details
Details for the file monad-0.1.tar.gz.
File metadata
- Download URL: monad-0.1.tar.gz
- Upload date:
- Size: 24.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
573423d97137f4b696424f53bd25838ad8c4ef38aae9b7f3667dfba11b87dfca
|
|
| MD5 |
035b82b6959362264416e851c3507e4d
|
|
| BLAKE2b-256 |
73075a2eeb496a862e1ce74f396e50404d6f84b882a5226203c08e9c751ca824
|