Skip to content

PyoSequence

Bases: PyoReversible[T], PyoCollection[T], Sequence[T]


              flowchart TD
              pyochain.abc._sequences.PyoSequence[PyoSequence]
              pyochain.abc._sequences.PyoReversible[PyoReversible]
              pyochain.abc._collection.PyoCollection[PyoCollection]
              pyochain.abc._iterable.PyoIterable[PyoIterable]
              pyochain.abc._collection.PyoContainer[PyoContainer]
              pyochain.abc._collection.PyoSized[PyoSized]
              pyochain.abc._mixins.Checkable[Checkable]
              pyochain.abc._mixins.Fluent[Fluent]
              pyochain.abc._mixins.Pipe[Pipe]
              pyochain.abc._mixins.Tap[Tap]

                              pyochain.abc._sequences.PyoReversible --> pyochain.abc._sequences.PyoSequence
                                pyochain.abc._iterable.PyoIterable --> pyochain.abc._sequences.PyoReversible
                                pyochain.abc._mixins.Checkable --> pyochain.abc._iterable.PyoIterable
                
                pyochain.abc._mixins.Fluent --> pyochain.abc._iterable.PyoIterable
                                pyochain.abc._mixins.Pipe --> pyochain.abc._mixins.Fluent
                
                pyochain.abc._mixins.Tap --> pyochain.abc._mixins.Fluent
                



                pyochain.abc._collection.PyoCollection --> pyochain.abc._sequences.PyoSequence
                                pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
                                pyochain.abc._mixins.Checkable --> pyochain.abc._iterable.PyoIterable
                
                pyochain.abc._mixins.Fluent --> pyochain.abc._iterable.PyoIterable
                                pyochain.abc._mixins.Pipe --> pyochain.abc._mixins.Fluent
                
                pyochain.abc._mixins.Tap --> pyochain.abc._mixins.Fluent
                


                pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
                                pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoContainer
                

                pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
                                pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoSized
                




              click pyochain.abc._sequences.PyoSequence href "" "pyochain.abc._sequences.PyoSequence"
              click pyochain.abc._sequences.PyoReversible href "" "pyochain.abc._sequences.PyoReversible"
              click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
              click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
              click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
              click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
              click pyochain.abc._mixins.Checkable href "" "pyochain.abc._mixins.Checkable"
              click pyochain.abc._mixins.Fluent href "" "pyochain.abc._mixins.Fluent"
              click pyochain.abc._mixins.Pipe href "" "pyochain.abc._mixins.Pipe"
              click pyochain.abc._mixins.Tap href "" "pyochain.abc._mixins.Tap"
            

Extends PyoCollection[T] and collections.abc.Sequence[T].

Is the shared ABC for concrete sequences: Seq, Range and Vec.

Any concrete subclass must implement the required Sequence dunder methods:

  • __getitem__
  • __len__
  • __contains__
  • __iter__
Example
from collections.abc import Iterator
from pyochain.abc import PyoSequence
from pyochain import Seq, Some

class MySeq(PyoSequence[int]):
    def __init__(self, data: list[int]):
        self._data = data
    def __getitem__(self, index: int) -> int:
        return self._data[index]

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

    def __contains__(self, item: int) -> bool:
        return item in self._data

    def __iter__(self) -> Iterator[int]:
        return iter(self._data)

my_seq = MySeq([10, 20, 30])
assert my_seq.first() == 10
assert my_seq.get(2) == Some(30)
Source code in pyochain/abc/_sequences.pyi
 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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
class PyoSequence[T](PyoReversible[T], PyoCollection[T], Sequence[T]):  # pyright: ignore[reportImplicitAbstractClass]
    """Extends `PyoCollection[T]` and `collections.abc.Sequence[T]`.

    Is the shared ABC for concrete sequences: `Seq`, `Range` and `Vec`.

    Any concrete subclass must implement the required `Sequence` dunder methods:

    - `__getitem__`
    - `__len__`
    - `__contains__`
    - `__iter__`

    Example:
        ```python
        from collections.abc import Iterator
        from pyochain.abc import PyoSequence
        from pyochain import Seq, Some

        class MySeq(PyoSequence[int]):
            def __init__(self, data: list[int]):
                self._data = data
            def __getitem__(self, index: int) -> int:
                return self._data[index]

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

            def __contains__(self, item: int) -> bool:
                return item in self._data

            def __iter__(self) -> Iterator[int]:
                return iter(self._data)

        my_seq = MySeq([10, 20, 30])
        assert my_seq.first() == 10
        assert my_seq.get(2) == Some(30)
        ```
    """
    @overload
    @abstractmethod
    def __getitem__(self, index: int, /) -> T: ...
    @overload
    @abstractmethod
    def __getitem__(self, index: slice[int | None], /) -> Sequence[T]: ...
    @override
    def __contains__(self, value: object, /) -> bool: ...
    @override
    def __iter__(self) -> Iterator[T]: ...
    @override
    def __reversed__(self) -> Iterator[T]: ...
    @override
    def index(self, value: Any, start: int = 0, stop: int = ..., /) -> int: ...  # pyright: ignore[reportAny]
    @override
    def count(self, value: Any, /) -> int: ...  # pyright: ignore[reportAny]
    def first(self) -> T:
        """Return the first element of the `Sequence`.

        Returns:
            T: The first element of the `Sequence`.

        Example:
            ```python
            from pyochain import Seq
            from pyochain.collections import StableSet

            data = Seq(1, 2)
            assert data.first() == 1
            # With an Iterator, the equivalent would be:
            assert data.iter().next().unwrap() == 1
            ```
        """

    def last(self) -> T:
        """Return the last element of the `Sequence`.

        This is similar to `my_sequence[-1]`.

        Returns:
            T: The last element of the `Sequence`.

        Example:
            ```python
            from pyochain import Seq

            assert Seq(1, 2, 3).last() == 3
            ```
        """

    @overload
    def get(self, index: int) -> Option[T]: ...
    @overload
    def get(self, index: slice[int | None]) -> Option[Sequence[T]]: ...
    def get(self, index: int | slice[int | None]) -> Option[T] | Option[Sequence[T]]:
        """Return the element at the specified index as `Some(value)`, or `None` if the index is out of bounds.

        Args:
            index (int | slice[int | None]): The index or slice of the element to retrieve.

        Returns:
            Option[T] | Option[Sequence[T]]: `Some(value)` if the index is valid, otherwise `None`.

        Example:
            ```python
            from pyochain import Seq, Some

            data = Seq(10, 20, 30)
            assert data.get(1) == Some(20)
            assert data.get(5).is_none()
            ```
        """

first()

Return the first element of the Sequence.

Returns:

Name Type Description
T T

The first element of the Sequence.

Example
from pyochain import Seq
from pyochain.collections import StableSet

data = Seq(1, 2)
assert data.first() == 1
# With an Iterator, the equivalent would be:
assert data.iter().next().unwrap() == 1
Source code in pyochain/abc/_sequences.pyi
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def first(self) -> T:
    """Return the first element of the `Sequence`.

    Returns:
        T: The first element of the `Sequence`.

    Example:
        ```python
        from pyochain import Seq
        from pyochain.collections import StableSet

        data = Seq(1, 2)
        assert data.first() == 1
        # With an Iterator, the equivalent would be:
        assert data.iter().next().unwrap() == 1
        ```
    """

last()

Return the last element of the Sequence.

This is similar to my_sequence[-1].

Returns:

Name Type Description
T T

The last element of the Sequence.

Example
from pyochain import Seq

assert Seq(1, 2, 3).last() == 3
Source code in pyochain/abc/_sequences.pyi
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def last(self) -> T:
    """Return the last element of the `Sequence`.

    This is similar to `my_sequence[-1]`.

    Returns:
        T: The last element of the `Sequence`.

    Example:
        ```python
        from pyochain import Seq

        assert Seq(1, 2, 3).last() == 3
        ```
    """

get(index)

get(index: int) -> Option[T]
get(index: slice[int | None]) -> Option[Sequence[T]]

Return the element at the specified index as Some(value), or None if the index is out of bounds.

Parameters:

Name Type Description Default
index int | slice[int | None]

The index or slice of the element to retrieve.

required

Returns:

Type Description
Option[T] | Option[Sequence[T]]

Option[T] | Option[Sequence[T]]: Some(value) if the index is valid, otherwise None.

Example
from pyochain import Seq, Some

data = Seq(10, 20, 30)
assert data.get(1) == Some(20)
assert data.get(5).is_none()
Source code in pyochain/abc/_sequences.pyi
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def get(self, index: int | slice[int | None]) -> Option[T] | Option[Sequence[T]]:
    """Return the element at the specified index as `Some(value)`, or `None` if the index is out of bounds.

    Args:
        index (int | slice[int | None]): The index or slice of the element to retrieve.

    Returns:
        Option[T] | Option[Sequence[T]]: `Some(value)` if the index is valid, otherwise `None`.

    Example:
        ```python
        from pyochain import Seq, Some

        data = Seq(10, 20, 30)
        assert data.get(1) == Some(20)
        assert data.get(5).is_none()
        ```
    """