mirror of
https://github.com/correl/typesafe-monads.git
synced 2024-11-15 03:00:21 +00:00
End of refactoring
This commit is contained in:
parent
07cd9a1237
commit
de5da7dbe4
8 changed files with 30 additions and 23 deletions
|
@ -63,9 +63,6 @@ class Future(Monad[T]):
|
|||
|
||||
__rshift__ = bind
|
||||
|
||||
def __and__(
|
||||
self, other: Awaitable[Callable[[T], S]]
|
||||
) -> Future[S]: # pragma: no cover
|
||||
return Future.apply(self, other)
|
||||
__and__ = lambda other, self: Future.apply(self, other) # type: ignore
|
||||
|
||||
__mul__ = __rmul__ = map
|
||||
|
|
|
@ -22,6 +22,7 @@ class List(Monad[T], Monoidal[list]):
|
|||
return List(list(map(function, self.value)))
|
||||
|
||||
def apply(self, functor: List[Callable[[T], S]]) -> List[S]:
|
||||
|
||||
return List(
|
||||
list(chain.from_iterable([map(f, self.value) for f in functor.value]))
|
||||
)
|
||||
|
@ -37,10 +38,10 @@ class List(Monad[T], Monoidal[list]):
|
|||
def mcons(acc: List[_List[T]], x: List[T]) -> List[_List[T]]:
|
||||
return acc.bind(lambda acc_: x.map(lambda x_: acc_ + [x_]))
|
||||
|
||||
return reduce(mcons, xs, List.mzero())
|
||||
empty: List[_List[T]] = List.pure([])
|
||||
return reduce(mcons, xs, empty)
|
||||
|
||||
def __and__(self, other: List[Callable[[T], S]]) -> List[S]: # pragma: no cover
|
||||
return List.apply(self, other)
|
||||
__and__ = lambda other, self: List.apply(self, other) # type: ignore
|
||||
|
||||
def mappend(self, other: List[T]) -> List[T]:
|
||||
return List(self.value + other.value)
|
||||
|
|
|
@ -86,8 +86,7 @@ class Maybe(Monad[T]):
|
|||
else:
|
||||
return Nothing()
|
||||
|
||||
def __and__(self, other: Maybe[Callable[[T], S]]) -> Maybe[S]: # pragma: no cover
|
||||
return Maybe.apply(self, other)
|
||||
__and__ = lambda other, self: Maybe.apply(self, other) # type: ignore
|
||||
|
||||
__rshift__ = bind
|
||||
__mul__ = __rmul__ = map
|
||||
|
|
|
@ -58,7 +58,8 @@ class Reader(Monad[T], Generic[Env, T]):
|
|||
def mcons(acc: Reader[Env, List[T]], x: Reader[Env, T]) -> Reader[Env, List[T]]:
|
||||
return acc.bind(lambda acc_: x.map(lambda x_: acc_ + [x_]))
|
||||
|
||||
empty: Reader[Env, List[T]] = cls.pure([])
|
||||
empty_list: List[T] = []
|
||||
empty: Reader[Env, List[T]] = Reader.pure(empty_list)
|
||||
return reduce(mcons, xs, empty)
|
||||
|
||||
def __eq__(self, other: object): # pragma: no cover
|
||||
|
@ -70,6 +71,7 @@ class Reader(Monad[T], Generic[Env, T]):
|
|||
signature = inspect.signature(self)
|
||||
return f"<Reader {module}.{name}{signature}>"
|
||||
|
||||
__and__ = lambda other, self: Reader.apply(self, other) # type: ignore
|
||||
|
||||
__mul__ = __rmul__ = map
|
||||
__rshift__ = bind
|
||||
__and__ = lambda other, self: Reader.apply(self, other)
|
||||
|
|
|
@ -60,7 +60,7 @@ class Result(Monad[T], Generic[T, E]):
|
|||
def mcons(acc: Result[List[T], E], x: Result[T, E]) -> Result[List[T], E]:
|
||||
return acc.bind(lambda acc_: x.map(lambda x_: acc_ + [x_]))
|
||||
|
||||
empty: Result[List[T], E] = cls.pure([])
|
||||
empty: Result[List[T], E] = Result.pure([])
|
||||
return functools.reduce(mcons, xs, empty)
|
||||
|
||||
def withDefault(self, default: T) -> T:
|
||||
|
@ -89,8 +89,9 @@ class Result(Monad[T], Generic[T, E]):
|
|||
else:
|
||||
return None
|
||||
|
||||
__and__ = lambda other, self: Result.apply(self, other) # type: ignore
|
||||
|
||||
__rshift__ = bind
|
||||
__and__ = lambda other, self: Result.apply(self, other)
|
||||
__mul__ = __rmul__ = map
|
||||
|
||||
|
||||
|
|
|
@ -3,3 +3,6 @@ test=pytest
|
|||
|
||||
[tool:pytest]
|
||||
addopts = --black --mypy --cov monads --cov-report xml:reports/coverage.xml
|
||||
|
||||
[mypy-tests]
|
||||
ignore_errors = True
|
|
@ -18,4 +18,5 @@ def test_apply_and_operator(monad) -> None:
|
|||
subtract: Callable[[int], Callable[[int], int]] = lambda x: lambda y: x - y
|
||||
ten = monad.pure(10)
|
||||
six = monad.pure(6)
|
||||
assert six.apply(ten.map(subtract)) == subtract * ten & six
|
||||
functor = ten.map(subtract)
|
||||
assert six.apply(functor) == subtract * ten & six
|
||||
|
|
|
@ -28,12 +28,15 @@ def test_curried_function_annotation_drops_arguments_as_it_is_applied() -> None:
|
|||
def add3(a: int, b: int, c: int) -> int:
|
||||
return a + b + c
|
||||
|
||||
assert inspect.Signature(
|
||||
[
|
||||
inspect.Parameter(
|
||||
param, inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int
|
||||
)
|
||||
for param in ["b", "c"]
|
||||
],
|
||||
return_annotation=int,
|
||||
) == inspect.signature(curry(add3)(1))
|
||||
assert (
|
||||
inspect.Signature(
|
||||
[
|
||||
inspect.Parameter(
|
||||
param, inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int
|
||||
)
|
||||
for param in ["b", "c"]
|
||||
],
|
||||
return_annotation=int,
|
||||
)
|
||||
== inspect.signature(curry(add3)(1))
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue