Example #1
0
 def optional(self) -> None:
     a = 'a'
     b = 'b'
     Right(a).to_maybe.should.just_contain(a)
     Left(a).to_maybe.should.be.a(Empty)
     Right(a).to_either(b).should.equal(Right(a))
     Left(a).to_either(b).should.equal(Left(a))
Example #2
0
 def map(self) -> None:
     a = 'a'
     b = 'b'
     Right(a).map(_ + b).value.should.equal(a + b)
     Left(a).map(_ + b).value.should.equal(a)
Example #3
0
 def f(z: int, a: int) -> Either[str, int]:
     return Right(z + a) if a < 5 else Left('too large')
Example #4
0
 def traverse(self) -> None:
     a = 'a'
     Right(Just(a)).sequence(Maybe).should.equal(Just(Right(a)))
     Left(Just(a)).sequence(Maybe).should.equal(Just(Left(Just(a))))
     List(Right(a)).sequence(Either).should.equal(Right(List(a)))
     List(Right(a), Left(a)).sequence(Either).should.equal(Left(a))
Example #5
0
 def flat_e(self, l, r):
     return call_by_name(r) if self else Left(call_by_name(l))
Example #6
0
 def flat_either_call(self, l, r):
     return r() if self else Left(l)
Example #7
0
 def e(self, l, r):
     return Right(call_by_name(r)) if self else Left(call_by_name(l))
Example #8
0
 def list_flat_map(self) -> None:
     (List(Right(1), Left(2), Right(3)).join).should.equal(List(1, 3))
Example #9
0
 def either_call(self, l, r):
     return Right(r()) if self else Left(l)
Example #10
0
 def to_either(self, fa: Maybe[A],
               left: Union[B, Callable[[], B]]) -> either.Either[A, B]:
     from amino.either import Left, Right
     return fa.cata(Right, lambda: Left(call_by_name(left)))
Example #11
0
 def e(self, f: A, t: B) -> Either[A, B]:
     return Right(call_by_name(t)) if self else Left(call_by_name(f))
Example #12
0
 def filter(self, fa: Either[A, B], f: Callable[[B], bool]) -> Either[Any, B]:
     return fa // (lambda a: Right(a) if f(a) else Left('filtered'))
Example #13
0
 def traverse(self, fa: Either[A, B], f: Callable, tpe: type) -> Any:
     monad = Applicative.fatal(tpe)
     r = lambda a: monad.map(f(a), Right)
     return fa.cata(lambda a: monad.pure(Left(a)), r)
Example #14
0
 def absent(self, msg: str) -> Either[str, B]:
     return Left(msg or 'not found')