Exemplo n.º 1
0
 def test_applying_with_left_in_second_arg(self):
     self.assertEqual(
         Either.apply(common_tests.add).to_arguments(Right(1), Left('')),
         Left(''))
Exemplo n.º 2
0
 def test_insert(self):
     self.assertEqual(Either.insert(1), Right(1))
Exemplo n.º 3
0
def pipeline(steps, value) -> Either:
    return reduce(lambda either, step: either.then(step), steps, Either.insert(value))
Exemplo n.º 4
0
 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'))))
Exemplo n.º 5
0
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)