Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Validation -> Maybe conversion method #20

Merged
merged 1 commit into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ A `Validation` instance includes the following methods:
if any, and returns the resulting `Validation`
* `ap(v)`: Applies the underlying value `a`, which must be a function,
to the value `v`, and returns the resulting `Validation`
* `to_maybe()`: Returns a `Just(a)` if this is a `Valid(a)`, and a
`Nothing` if this is an `Invalid(es)`

The `validation` module includes the following functions:

Expand Down
6 changes: 6 additions & 0 deletions pygow/validation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import maybe

class Valid:
a = None
def __init__(self, a):
Expand All @@ -20,6 +22,8 @@ def ap(self, v):
return Valid(self.a(v.a))
else:
return v
def to_maybe(self):
return maybe.Just(self.a)

class Invalid:
es = None
Expand All @@ -43,6 +47,8 @@ def ap(self, v):
return self
else:
return Invalid(self.es + v.es)
def to_maybe(self):
return maybe.Nothing()

def get_required_env(name):
from os import getenv
Expand Down
6 changes: 5 additions & 1 deletion tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from nose.tools import assert_true, assert_false, assert_equals
from pygow import validation
from pygow import validation, maybe

def multBy(x):
def k(y):
Expand Down Expand Up @@ -46,6 +46,10 @@ def test_get_required_env():
assert_true(validation.get_required_env('HOME').is_valid())
assert_false(validation.get_required_env('THIS_AINT_AN_ENV_VAR').is_valid())

def test_to_maybe():
assert_equals(maybe.Just(42), validation.Valid(42).to_maybe())
assert_equals(maybe.Nothing(), validation.Invalid(['nope']).to_maybe())

a_v = validation.Valid('a')
a_i = validation.Invalid(['invalid: a'])
def test_lift_a():
Expand Down