Functional data structures for Python.
Usage
$ pip install pygow==0.5
Features
Maybe
A Maybe
represents an optional value. It can be either Nothing
, or
Just(a)
for some value a
.
A Maybe
instance includes the following methods:
-
is_just()
: Returns true if this is aJust
, or false if it is aNothing
-
map(f)
: Applies the functionf
to the underlying valuea
, if any, and returns a newMaybe
containing it -
flat_map(f)
: Applies the functionf
to the underlying valuea
, if any, and returns the resultingMaybe
instance -
get_or_else(x)
: Returns the underlying valuea
, if any, otherwise returns the parameterx
-
or_else(x)
: Returns thisMaybe
instance if it is aJust
, otherwise returns the parameterx
-
to_validation(e)
: Returns aValid(a)
if this is aJust(a)
, orInvalid([e])
if this is aNothing
The maybe
module includes the following functions:
-
get_maybe_env(name)
: Returns aMaybe
of the named environment variable -
non_empty_string(x)
: Returns aJust
of the stringx
if it is non-empty, orNothing
otherwise -
parse_int(x)
: Returns aJust
of the integer value of the stringx
, if it is parseable, orNothing
otherwise -
maybe(x)
: Constructs aMaybe
instance around the parameterx
, returningJust
ifx
is notNone
, andNothing
otherwise
Validation
A Validation
represents the result of a validation operation. It can
be either Valid(a)
for some value a
, or Invalid(es)
for some list
of errors es
.
A Validation
instance includes the following methods:
-
is_valid()
: Returns true if this is aValid
, or false if it is anInvalid
-
map(f)
: Applies the functionf
to the underlying valuea
, if any, and returns a newValidation
containing it -
flat_map(f)
: Applies the functionf
to the underlying valuea
, if any, and returns the resultingValidation
-
ap(v)
: Applies the underlying valuea
, which must be a function, to the valuev
, and returns the resultingValidation
-
to_maybe()
: Returns aJust(a)
if this is aValid(a)
, and aNothing
if this is anInvalid(es)
The validation
module includes the following functions:
-
get_required_env(name)
: Returns aValidation
of the named environment variable -
lift_a(f)
: Returns aValid
of the unary functionf
-
lift_aN(n, f)
: Returns aValid
of a curried representation of the N-arity functionf
.
Examples
See the tests for complete examples.
Development
To release a new version of pygow:
-
Configure ~/.pypirc:
[server-login] username: YOUR_PYPI_USERNAME password: YOUR_PYPI_PASSWORD [pypi] repository: https://pypi.python.org/pypi [pypitest] repository: https://testpypi.python.org/pypi
-
Update the version number in setup.py
-
Commit all changes
-
Add a version tag to git:
git tag 0.1
-
Publish to PyPI:
python setup.py sdist upload -r pypi