smonad - a functional library
Project description
Introduction
What?
Monads in python, with some helpful functions.
This package is a fork of Philip Xu’s excellent monad package. Philip’s package has been modified to be more approachable for Python developers. It takes some inspiration from the excellent lambda library for Java and Scala’s Try monad
How?
>>> from smonad.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 smonad.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 smonad
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:
- 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
smonad-0.5.tar.gz
(24.2 kB
view hashes)