Skip to content

Traverable

ListTraversable

This class implements the Traversable behavior for the built-in list type.

Source code in funclift/traverable.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class ListTraversable():
    """This class implements the Traversable behavior for the built-in list type."""

    @staticmethod
    def traverse(ls: list[A],
                 f: Callable[[A], Applicative[AP, B]]) -> AP_LB:
        sig = signature(f)
        mcls = sig.return_annotation
        return ListTraversable.traverse_left(mcls, ls, f)

    @staticmethod
    def traverse_right(mcls, ls: list[A],
                       f: Callable[[A], Applicative[AP, B]]) -> AP_LB:
        def fold_func(a: A, bs: AP_LB) -> AP_LB:
            return mcls.pure(colon_right).ap(f(a)).ap(bs)

        return ListFoldable.fold_right(ls, fold_func, mcls.pure([]))

    @staticmethod
    def traverse_left(mcls, ls: list[A],
                      f: Callable[[A], Applicative[AP, B]]) -> AP_LB:
        # mcls is the effect class
        def fold_func(bs: AP_LB, a: A) -> AP_LB:
            return mcls.pure(colon_left).ap(bs).ap(f(a))

        return ListFoldable.fold_left(ls, fold_func, mcls.pure([]))

    @staticmethod
    def sequence(mcls, list_ap_a: list[Applicative[AP, B]]) -> AP_LB:
        # mcls is the effect class
        def id_func(a: Applicative[AP, B]) -> Applicative[AP, B]:
            return a

        return ListTraversable.traverse_left(mcls, list_ap_a, id_func)

Traversable

Bases: Generic[F, A], Protocol

Traversable F is a Functor and also a Foldable.

Need to provide default implementations for the following two methods: mapM :: Monad m => (a -> m b) -> t a -> m (t b) sequence :: Monad m => t (m a) -> m (t a)

Source code in funclift/traverable.py
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
class Traversable(Generic[F, A], Protocol):
    """Traversable
    F is a Functor and also a Foldable.

    Need to provide default implementations for the following two methods:
    mapM      :: Monad m => (a -> m b) -> t a -> m (t b)
    sequence  :: Monad m => t (m a) -> m (t a)
    """

    def traverse(self,
                 f: Callable[[A], Applicative[AP, B]]) -> Applicative[AP, Functor[F, B]]:
        """
        AP[B] is an Applicative.
        traverse :: F[A] -> (A -> AP[B]) -> AP[F[B]]

        Args:
            f (Callable[[A], Applicative[AP, B]]): _description_

        Returns:
            Applicative[AP, Functor[F, B]]: _description_
        """
        ...

    def sequence(self, f_ap_a: Functor[F, Applicative[AP, A]]) -> Applicative[AP, Functor[F, A]]:
        # This is called sequenceA in Haskell.
        # sequenceA F [AP[A]] -> AP [F[A]]
        ...

traverse(f)

AP[B] is an Applicative. traverse :: F[A] -> (A -> AP[B]) -> AP[F[B]]

Parameters:

Name Type Description Default
f Callable[[A], Applicative[AP, B]]

description

required

Returns:

Type Description
Applicative[AP, Functor[F, B]]

Applicative[AP, Functor[F, B]]: description

Source code in funclift/traverable.py
29
30
31
32
33
34
35
36
37
38
39
40
41
def traverse(self,
             f: Callable[[A], Applicative[AP, B]]) -> Applicative[AP, Functor[F, B]]:
    """
    AP[B] is an Applicative.
    traverse :: F[A] -> (A -> AP[B]) -> AP[F[B]]

    Args:
        f (Callable[[A], Applicative[AP, B]]): _description_

    Returns:
        Applicative[AP, Functor[F, B]]: _description_
    """
    ...