def test_insert(self): self.assertEqual(Either.insert(1), Right(1))
def pipeline(steps, value) -> Either: return reduce(lambda either, step: either.then(step), steps, Either.insert(value))
def test_exceptions_wrapped_in_left_kleisli_function(self): self.assertEqual(str(Either.insert(1).then(lambda x: Right(x / 0))), str(Left(ZeroDivisionError('division by zero'))))
def handleError(e: Exception): if isinstance(e, TooHigh): return "Value too high" elif isinstance(e, Odd): return "The number must be even" else: raise e @curry(2) def pipeline(steps, value) -> Either: return reduce(lambda either, step: either.then(step), steps, Either.insert(value)) def getValue(value): return value if __name__ == "__main__": # myValue = Either.insert(2)\ # .then(mustBeEven)\ # .then(mustBeLowerThan(5))\ # .either(handleError, getValue) process = pipeline([mustBeEven, mustBeLowerThan(5)]) myValue = Either.insert(2).then(process).either(handleError, getValue) print(myValue)