Skip to content

ZipList

funstruct.applicative.ziplist

ZipList — a list with element-wise applicative semantics.

Examples:

>>> from funstruct.applicative.ziplist import ZipList
>>> ZipList([1, 2, 3]).map(lambda x: x * 10)
ZipList([10, 20, 30])
>>> ZipList([lambda x: x + 1]).ap(ZipList([9]))
ZipList([10])
>>> ZipList([lambda x: x + 1, lambda x: x * 2]).ap(ZipList([10, 20]))
ZipList([11, 40])
>>> ZipList([1, 2]) * ZipList([3, 4])
ZipList([(1, 3), (2, 4)])

ZipList

Bases: DataType, Generic[_A]

List with element-wise applicative.

Source code in funstruct/applicative/ziplist/__init__.py
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
class ZipList(DataType, Generic[_A]):
    """List with element-wise applicative."""

    def __init__(self, values: Iterable[_A]) -> None:
        self._values = list(values)

    @classmethod
    def pure(cls, value: _A) -> ZipList[_A]:
        return cls([value])

    def __mul__(self, other: ZipList) -> ZipList:
        return self.product(other)

    def to_list(self) -> list[_A]:
        return list(self._values)

    def __iter__(self) -> Iterator[_A]:
        return iter(self._values)

    def __len__(self) -> int:
        return len(self._values)

    def __eq__(self, other: object) -> bool:
        match other:
            case ZipList():
                return self._values == other._values
            case list():
                return self._values == other
            case _:
                return False

    def __repr__(self) -> str:
        return f"ZipList({self._values})"