Skip to content

Either

Either

Bases: Generic[L, R]

The Either class represents one of two cases: left or right. The left case usually represents an error and is modeled by the Left class. The right case represents a success and is modeled by the Right class.

Either is a Monad. It is parameterized by two type variables: L and R. The type variable L is the value type of the left case. The type variable R is the value type of the right case. Either is monadic in the type varaible R.

Source code in funclift/types/either.py
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
60
61
62
63
64
65
66
67
68
class Either(Generic[L, R]):
    """
    The Either class represents one of two cases: left or right. The left case
    usually represents an error and is modeled by the
    [`Left`][funclift.types.either.Left] class. The right case represents a
    success and is modeled by the [`Right`][funclift.types.either.Right]
    class.

    Either is a [`Monad`][funclift.monad.Monad]. It is parameterized by
    two type variables: L and R. The type variable L is the value type of the
    left case. The type variable R is the value type of the right case.
    Either is monadic in the type varaible R.
    """

    @staticmethod
    def pure(a: E) -> Right[E]:
        """Creates a [`Right`][funclift.types.either.Right] instance to represent a success case whose value
        is the passed-in parameter.

        Args:
            a (E): value of the success case.

        Returns:
            Right[E]: a [`Right`][funclift.types.either.Right] instance that represents a success case.
        """
        return Right(a)

    @abstractmethod
    def fmap(self, f: Callable[[R], B]) -> Either[L, B]:
        """
        Need to repeat this to make mypy happy or else mypy will think
        the return type is Functor[B].

        Args:
            f (Callable[[R], B]): _description_

        Returns:
            Either[L, B]: _description_
        """
        ...

    @abstractmethod
    def flatmap(self, f: Callable[[R], Either[L, B]]) -> Either[L, B]:
        """_summary_

        Args:
            f (Callable[[R], Either[L, B]]): _description_

        Returns:
            Either[L, B]: _description_
        """
        ...

flatmap(f) abstractmethod

summary

Parameters:

Name Type Description Default
f Callable[[R], Either[L, B]]

description

required

Returns:

Type Description
Either[L, B]

Either[L, B]: description

Source code in funclift/types/either.py
58
59
60
61
62
63
64
65
66
67
68
@abstractmethod
def flatmap(self, f: Callable[[R], Either[L, B]]) -> Either[L, B]:
    """_summary_

    Args:
        f (Callable[[R], Either[L, B]]): _description_

    Returns:
        Either[L, B]: _description_
    """
    ...

fmap(f) abstractmethod

Need to repeat this to make mypy happy or else mypy will think the return type is Functor[B].

Parameters:

Name Type Description Default
f Callable[[R], B]

description

required

Returns:

Type Description
Either[L, B]

Either[L, B]: description

Source code in funclift/types/either.py
44
45
46
47
48
49
50
51
52
53
54
55
56
@abstractmethod
def fmap(self, f: Callable[[R], B]) -> Either[L, B]:
    """
    Need to repeat this to make mypy happy or else mypy will think
    the return type is Functor[B].

    Args:
        f (Callable[[R], B]): _description_

    Returns:
        Either[L, B]: _description_
    """
    ...

pure(a) staticmethod

Creates a Right instance to represent a success case whose value is the passed-in parameter.

Parameters:

Name Type Description Default
a E

value of the success case.

required

Returns:

Type Description
Right[E]

Right[E]: a Right instance that represents a success case.

Source code in funclift/types/either.py
31
32
33
34
35
36
37
38
39
40
41
42
@staticmethod
def pure(a: E) -> Right[E]:
    """Creates a [`Right`][funclift.types.either.Right] instance to represent a success case whose value
    is the passed-in parameter.

    Args:
        a (E): value of the success case.

    Returns:
        Right[E]: a [`Right`][funclift.types.either.Right] instance that represents a success case.
    """
    return Right(a)

Left dataclass

Bases: Either[L, Any]

Left

Source code in funclift/types/either.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@dataclass
class Left(Either[L, Any]):
    """Left"""

    value: L
    mtype: ClassVar[type] = Either

    def fmap(self, f: Callable[[Any], Any]) -> Left[L]:
        return self

    def flatmap(self, f: Callable[[Any], Either[L, B]]) -> Either[L, B]:
        return self

    def ap(self, other: Either[L, R]) -> Either[L, E]:
        return self

Right dataclass

Bases: Either[Any, A]

Right

Source code in funclift/types/either.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@dataclass
class Right(Either[Any, A]):
    """Right"""

    value: A
    mtype: ClassVar[type] = Either

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

    def flatmap(self, f: Callable[[A], Either[L, B]]) -> Either[L, B]:
        return f(self.value)

    def ap(self: Right[Callable[[R], E]], other: Either[L, R]) -> Either[L, E]:
        return other.fmap(self.value)