Skip to content

Validated

Invalid dataclass

Bases: Validated[E, Any]

Invalid

Source code in funclift/types/validated.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@dataclass
class Invalid(Validated[E, Any]):
    """Invalid"""

    error: E

    def fmap(self, f: Callable[[A], B]) -> Invalid[E]:
        return self

    def ap(self, a: Validated[E, C]) -> Invalid[E]:
        """

        Args:
            a (Validated[E, C]): _description_

        Returns:
            Invalid[E]: _description_
        """
        if isinstance(a, Invalid):
            return Invalid(self.error + a.error)

        return self

ap(a)

Parameters:

Name Type Description Default
a Validated[E, C]

description

required

Returns:

Type Description
Invalid[E]

Invalid[E]: description

Source code in funclift/types/validated.py
65
66
67
68
69
70
71
72
73
74
75
76
77
def ap(self, a: Validated[E, C]) -> Invalid[E]:
    """

    Args:
        a (Validated[E, C]): _description_

    Returns:
        Invalid[E]: _description_
    """
    if isinstance(a, Invalid):
        return Invalid(self.error + a.error)

    return self

Valid dataclass

Bases: Validated[Any, A]

Valid

Source code in funclift/types/validated.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@dataclass
class Valid(Validated[Any, A]):
    """Valid"""

    value: A

    def get(self) -> A | None:
        return self.value

    @staticmethod
    def pure(a: D) -> Valid[D]:
        return Valid(a)

    def fmap(self, f: Callable[[A], B]) -> Valid[B]:
        return Valid(f(self.value))

    def ap(self: Valid[Callable[[C], D]],
           a: Validated[E, C]) -> Validated[E, D]:
        if isinstance(a, Invalid):
            return a

        return a.fmap(self.value)

Validated

Bases: Generic[E, A]

Validated

Source code in funclift/types/validated.py
17
18
19
20
21
22
23
24
25
26
27
28
29
class Validated(Generic[E, A]):
    """Validated"""

    @staticmethod
    def pure(a: D) -> Valid[D]:
        return Valid(a)

    @abstractmethod
    def fmap(self, f: Callable[[A], B]) -> Validated[E, B]: ...

    @abstractmethod
    def ap(self: Validated[E, Callable[[C], D]],
           a: Validated[E, C]) -> Validated[E, D]: ...