Either¶
from pymonet.either import Left, Right
from pymonet.utils import identity
def divide(divided, divider):
if divider == 0:
return Left('can not divide by 0')
return Right(divided, divider)
def handle_error(value):
print ('error {}'.format(value))
def handle_success(value):
print ('success {}'.format(value))
(divide(42, 0)
.map(lambda value: value + 1)
.bind(lambda value: Right(value + 1))
.case(error=handle_error, success=handle_success))
# error 42
(divide(42, 1)
.map(identity, lambda value: value + 1)
.bind(lambda value: Right(value + 1))
.case(error=handle_error, success=handle_success))
# success 44
-
class
pymonet.either.
Left
(value: T)¶ Not successfully Either
-
bind
(_) → pymonet.either.Left[~T][T]¶ Take mapper function and return value of Left.
- Returns
Stored value
- Return type
A
-
is_left
() → bool¶ - Returns
True
- Return type
Boolean
-
is_right
() → bool¶ - Returns
False
- Return type
Boolean
-
map
(_: Callable[[Any], Any]) → pymonet.either.Left[~T][T]¶ Take mapper function and return new instance of Left with the same value.
- Returns
Copy of self
- Return type
Left[A]
-
to_lazy
()¶ Transform Either to Try.
- Returns
Lazy monad with function returning previous value
- Return type
Lazy[Function() -> A]
-
to_try
()¶ Transform Either to Try.
- Returns
resolved Try monad with previous value. Right is resolved successfully, Left not.
- Return type
Box[A]
-
to_validation
()¶ Transform Box into Validation.
- Returns
failed Validation monad with previous value as error
- Return type
Validation[None, [A]]
-
-
class
pymonet.either.
Right
(value: T)¶ Not successfully Either
-
ap
(applicative)¶ Applies the function inside the Either[A] structure to another applicative type.
- Parameters
applicative (Either[B]) – applicative contains function
- Returns
new Either with result of contains function
- Return type
Either[A(B)]
-
bind
(mapper: Callable[[T], U]) → U¶ Take mapper function and returns result of them called with Right value.
- Parameters
mapper (Function(A) -> Either[B]) – function to apply on Right value
- Returns
result of mapper
- Return type
Either[B]
-
is_left
() → bool¶ - Returns
False
- Return type
Boolean
-
is_right
() → bool¶ - Returns
True
- Return type
Boolean
-
map
(mapper: Callable[[T], U]) → pymonet.either.Either[~U][U]¶ Take mapper function and return new instance of Right with mapped value.
- Parameters
mapper (Function(A) -> B) – function to apply on Right value
- Returns
new Right with result of mapper
- Return type
Right[B]
-
to_lazy
()¶ Transform Either to Try.
- Returns
Lazy monad with function returning previous value
- Return type
Lazy[Function() -> A]
-
to_try
()¶ Transform Either to Try.
- Returns
resolved Try monad with previous value. Right is resolved successfully, Left not.
- Return type
Box[A]
-
to_validation
()¶ Transform Either into Validation.
- Returns
successfull Validation monad with previous value
- Return type
Validation[A, []]
-