either
Module Reference
do
Bases: Generic[L]
Lift a function with do notation
@do[LeftType]()
def myfn(e: Either[LeftType, int]):
value = e.unsafe()
return value + 1
myfn(Left('err')) # Left('err')
myfn(Right(1)) # Right(2)
Source code in haskellian/src/haskellian/either/do_notation.py
safe
Bases: Generic[Err]
A decorator to catch exceptions and return them as Left
.
@E.safe[OSError]()
def safe_write(path: str, content: bytes):
with open(path, 'wb') as f:
f.write(content)
result = safe_write('file.txt', b'content') # Either[OSError, None]
Source code in haskellian/src/haskellian/either/funcs.py
filter(eithers)
filter_lefts(eithers)
maybe(x)
sequence(eithers)
List of lefts if any, otherwise list of all rights.
E.sequence([Left(1), Right(2), Right(3), Left(4)]) # Left([1, 4])
E.sequence([Right(2), Right(3)]) # Right([2, 3])