Skip to content

Foldable

Foldable

Bases: Protocol

A foldable can be reduced to a value.

Source code in funclift/foldable.py
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
class Foldable(Protocol):
    """A foldable can be reduced to a value."""

    def fold_right(self, f: Callable[[A, B], B]) -> B:
        """Reduces a foldable in a right-associative manner.

        Args:
            self: A foldable structure that contains values of type A.
            f (Callable[[A, B], B]): A function to be used while folding the
            structure.

        Returns:
            B: The value the structure reduces to.
        """
        ...

    def fold_left(self, f: Callable[[B, A], B]) -> B:
        """Reduces a foldable in a left-associative manner.

        Args:
            self: A foldable structure that contains values of type A.
            f (Callable[[B, A], B]): A function to be used while folding the
            structure.

        Returns:
            B: The value the structure reduces to.
        """
        ...

    def fold_map(self, f: Callable[[A], M]) -> M:
        """_summary_

        Args:
            f (Callable[[A], M]): _description_

        Returns:
            M: _description_
        """
        ...

fold_left(f)

Reduces a foldable in a left-associative manner.

Parameters:

Name Type Description Default
self

A foldable structure that contains values of type A.

required
f Callable[[B, A], B]

A function to be used while folding the

required

Returns:

Name Type Description
B B

The value the structure reduces to.

Source code in funclift/foldable.py
32
33
34
35
36
37
38
39
40
41
42
43
def fold_left(self, f: Callable[[B, A], B]) -> B:
    """Reduces a foldable in a left-associative manner.

    Args:
        self: A foldable structure that contains values of type A.
        f (Callable[[B, A], B]): A function to be used while folding the
        structure.

    Returns:
        B: The value the structure reduces to.
    """
    ...

fold_map(f)

summary

Parameters:

Name Type Description Default
f Callable[[A], M]

description

required

Returns:

Name Type Description
M M

description

Source code in funclift/foldable.py
45
46
47
48
49
50
51
52
53
54
def fold_map(self, f: Callable[[A], M]) -> M:
    """_summary_

    Args:
        f (Callable[[A], M]): _description_

    Returns:
        M: _description_
    """
    ...

fold_right(f)

Reduces a foldable in a right-associative manner.

Parameters:

Name Type Description Default
self

A foldable structure that contains values of type A.

required
f Callable[[A, B], B]

A function to be used while folding the

required

Returns:

Name Type Description
B B

The value the structure reduces to.

Source code in funclift/foldable.py
19
20
21
22
23
24
25
26
27
28
29
30
def fold_right(self, f: Callable[[A, B], B]) -> B:
    """Reduces a foldable in a right-associative manner.

    Args:
        self: A foldable structure that contains values of type A.
        f (Callable[[A, B], B]): A function to be used while folding the
        structure.

    Returns:
        B: The value the structure reduces to.
    """
    ...

ListFoldable

Implements Foldable for lists.

Source code in funclift/foldable.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class ListFoldable:
    """Implements [Foldable][funclift.foldable.Foldable] for lists."""

    @staticmethod
    def fold_right(fa: list[A], f: Callable[[A, B], B], id: B) -> B:
        acc = id
        for a in reversed(fa):
            acc = f(a, acc)
        return acc

    @staticmethod
    def fold_left(fa: list[A], f: Callable[[B, A], B], id: B) -> B:
        acc = id
        for a in fa:
            acc = f(acc, a)
        return acc

    @staticmethod
    def fold_map(fa: list[A], f: Callable[[A], M]) -> M:
        sig = signature(f)
        mcls = sig.return_annotation
        acc: M = get_mempty(mcls)

        for a in fa:
            m = f(a)
            acc = acc + m
        return acc

    @staticmethod
    def fold_map2(mcls: type, fa: list[A], f: Callable[[A], M]) -> M:
        acc: M = get_mempty(mcls)

        for a in fa:
            m = f(a)
            acc = acc + m
        return acc