-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathidentity.py
More file actions
59 lines (43 loc) · 1.7 KB
/
Copy pathidentity.py
File metadata and controls
59 lines (43 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""The identity monad.
Cf. the identity function. This is a no-op — just regular function
composition dressed as a monad. Its value is pedagogical: it shows the
monad structure in its simplest form, and serves as a sanity reference
when building other monads.
"""
__all__ = ["Identity"]
from collections.abc import Callable
from typing import Any
from .abc import LiftableMonad
class Identity(LiftableMonad):
"""The identity monad.
Binding through ``Identity`` is the same as ordinary function
composition: ``Identity(x) >> f == f(x)`` (where ``f: a -> M b``).
Usage::
from unpythonic.monads import Identity
result = Identity(2) >> (lambda x: Identity(x + 1))
# result == Identity(3)
"""
def __init__(self, x: Any) -> None:
"""Unit: wrap a plain value ``x: a`` into ``Identity a``."""
self.x = x
def fmap(self, f: Callable) -> "Identity":
"""``fmap: Identity a -> (a -> b) -> Identity b``"""
cls = self.__class__
return cls(f(self.x))
def join(self) -> "Identity":
"""``join: Identity (Identity a) -> Identity a``"""
cls = self.__class__
if not isinstance(self.x, cls):
raise TypeError(f"Expected a nested {cls.__name__}, got {type(self.x)} with data {self.x!r}")
return self.x
def __eq__(self, other: Any) -> bool:
if other is self:
return True
if not isinstance(other, Identity):
return NotImplemented
return self.x == other.x
def __hash__(self) -> int:
return hash((Identity, self.x))
def __repr__(self) -> str: # pragma: no cover
return f"{self.__class__.__name__}({self.x!r})"