mirror of
https://github.com/correl/typesafe-monads.git
synced 2024-11-21 19:18:42 +00:00
Rename methods
unit -> pure fmap -> map
This commit is contained in:
parent
f5b0e6e5e4
commit
c71b9d6ff6
6 changed files with 22 additions and 22 deletions
|
@ -6,5 +6,5 @@ S = TypeVar("S")
|
|||
|
||||
|
||||
class Functor(Generic[T]):
|
||||
def fmap(self, function: Callable[[T], S]) -> Functor[S]:
|
||||
def map(self, function: Callable[[T], S]) -> Functor[S]:
|
||||
raise NotImplementedError
|
||||
|
|
|
@ -13,7 +13,7 @@ class Maybe(Monad[T]):
|
|||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def unit(cls, value: T) -> Maybe[T]:
|
||||
def pure(cls, value: T) -> Maybe[T]:
|
||||
return Just(value)
|
||||
|
||||
def bind(self, function: Callable[[T], Maybe[S]]) -> Maybe[S]:
|
||||
|
@ -23,7 +23,7 @@ class Maybe(Monad[T]):
|
|||
new: Maybe[S] = Nothing()
|
||||
return new
|
||||
|
||||
def fmap(self, function: Callable[[T], S]) -> Maybe[S]:
|
||||
def map(self, function: Callable[[T], S]) -> Maybe[S]:
|
||||
if isinstance(self, Just):
|
||||
return Just(function(self.value))
|
||||
else:
|
||||
|
@ -37,7 +37,7 @@ class Maybe(Monad[T]):
|
|||
return default
|
||||
|
||||
__rshift__ = bind
|
||||
__mul__ = __rmul__ = fmap
|
||||
__mul__ = __rmul__ = map
|
||||
|
||||
|
||||
class Just(Maybe[T]):
|
||||
|
|
|
@ -9,7 +9,7 @@ S = TypeVar("S")
|
|||
|
||||
class Monad(Functor[T]):
|
||||
@classmethod
|
||||
def unit(cls, value: T) -> Monad[T]:
|
||||
def pure(cls, value: T) -> Monad[T]:
|
||||
raise NotImplementedError
|
||||
|
||||
# FIXME: Callable return type set to Any, as the proper value
|
||||
|
|
|
@ -8,7 +8,7 @@ E = TypeVar("E")
|
|||
|
||||
class Monad(Generic[T]):
|
||||
@classmethod
|
||||
def unit(cls, value: T) -> Monad[T]:
|
||||
def pure(cls, value: T) -> Monad[T]:
|
||||
raise NotImplementedError
|
||||
|
||||
def bind(self, function: Callable[[T], Any]) -> Monad[S]:
|
||||
|
@ -20,7 +20,7 @@ class Result(Monad[T], Generic[T, E]):
|
|||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def unit(cls, value: T) -> Result[T, E]:
|
||||
def pure(cls, value: T) -> Result[T, E]:
|
||||
return Ok(value)
|
||||
|
||||
def bind(self, function: Callable[[T], Result[S, E]]) -> Result[S, E]:
|
||||
|
@ -32,9 +32,9 @@ class Result(Monad[T], Generic[T, E]):
|
|||
else:
|
||||
raise TypeError
|
||||
|
||||
def fmap(self, function: Callable[[T], S]) -> Result[S, E]:
|
||||
def map(self, function: Callable[[T], S]) -> Result[S, E]:
|
||||
if isinstance(self, Ok):
|
||||
return Result.unit(function(self.value))
|
||||
return Result.pure(function(self.value))
|
||||
elif isinstance(self, Err):
|
||||
new: Result[S, E] = Err(self.err)
|
||||
return new
|
||||
|
@ -48,7 +48,7 @@ class Result(Monad[T], Generic[T, E]):
|
|||
return default
|
||||
|
||||
__rshift__ = bind
|
||||
__mul__ = __rmul__ = fmap
|
||||
__mul__ = __rmul__ = map
|
||||
|
||||
|
||||
class Ok(Result[T, E]):
|
||||
|
|
|
@ -9,31 +9,31 @@ def monad(request) -> Type:
|
|||
|
||||
|
||||
def test_bind(monad) -> None:
|
||||
expected: Monad[int] = monad.unit(2)
|
||||
assert expected == monad.unit(1).bind(lambda x: monad.unit(x + 1))
|
||||
expected: Monad[int] = monad.pure(2)
|
||||
assert expected == monad.pure(1).bind(lambda x: monad.pure(x + 1))
|
||||
|
||||
|
||||
def test_left_identity(monad) -> None:
|
||||
n: int = 3
|
||||
|
||||
def f(n: int) -> Monad[int]:
|
||||
return monad.unit(n * 3)
|
||||
return monad.pure(n * 3)
|
||||
|
||||
assert monad.unit(n).bind(f) == f(n)
|
||||
assert monad.pure(n).bind(f) == f(n)
|
||||
|
||||
|
||||
def test_right_identity(monad) -> None:
|
||||
m: Monad[int] = monad.unit(3)
|
||||
assert m == m.bind(lambda x: monad.unit(x))
|
||||
m: Monad[int] = monad.pure(3)
|
||||
assert m == m.bind(lambda x: monad.pure(x))
|
||||
|
||||
|
||||
def test_associativity(monad) -> None:
|
||||
m: Monad[int] = monad.unit(3)
|
||||
m: Monad[int] = monad.pure(3)
|
||||
|
||||
def f(n: int) -> Monad[int]:
|
||||
return monad.unit(n * 3)
|
||||
return monad.pure(n * 3)
|
||||
|
||||
def g(n: int) -> Monad[int]:
|
||||
return monad.unit(n + 5)
|
||||
return monad.pure(n + 5)
|
||||
|
||||
assert m.bind(f).bind(g) == m.bind(lambda x: f(x).bind(g))
|
||||
|
|
|
@ -12,13 +12,13 @@ def test_error() -> None:
|
|||
assert 0 == result.withDefault(0)
|
||||
|
||||
|
||||
def test_fmap() -> None:
|
||||
def test_map() -> None:
|
||||
result: Result[int, str] = Ok(5)
|
||||
mapped: Result[str, str] = result.fmap(str)
|
||||
mapped: Result[str, str] = result.map(str)
|
||||
assert "5" == mapped.withDefault("0")
|
||||
|
||||
|
||||
def test_fmap_infix() -> None:
|
||||
def test_map_infix() -> None:
|
||||
result: Result[int, str] = Ok(5)
|
||||
mapped: Result[str, str] = result * str
|
||||
assert "5" == mapped.withDefault("0")
|
||||
|
|
Loading…
Reference in a new issue