Skip to content

PyoSequence

Bases: PyoCollection[T], Sequence[T]

Base trait for eager pyochain sequences.

PyoSequence[T] is the shared trait for concrete, eager sequence-like collections: Seq and Vec.

It extends PyoCollection[T] and collections.abc.Sequence[T].

This is equivalent to subclassing collections.abc.Sequence[T] (this trait already does), meaning any concrete subclass must implement the required Sequence dunder methods:

  • __getitem__
  • __len__
  • __contains__
  • __iter__

On top of the standard Sequence protocol, it provides the additional pyochain API (from PyoCollection, Pipeable, Checkable, plus any helpers defined here).

Source code in src/pyochain/traits/_iterable.py
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
class PyoSequence[T](PyoCollection[T], Sequence[T]):
    """Base trait for eager pyochain sequences.

    `PyoSequence[T]` is the shared trait for concrete, eager sequence-like
    collections: `Seq` and `Vec`.

    It extends `PyoCollection[T]` and `collections.abc.Sequence[T]`.

    This is equivalent to subclassing `collections.abc.Sequence[T]` (this trait
    already does), meaning any concrete subclass must implement the required
    `Sequence` dunder methods:

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

    On top of the standard `Sequence` protocol, it provides the additional
    pyochain API (from `PyoCollection`, `Pipeable`, `Checkable`, plus any helpers defined here).

    """

    __slots__ = ()

    @overload
    def get(self, index: int) -> Option[T]: ...
    @overload
    def get(self, index: slice) -> Option[Sequence[T]]: ...
    def get(self, index: int | slice) -> 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): 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
        >>> import pyochain as pc
        >>> pc.Seq([10, 20, 30]).get(1)
        Some(20)
        >>> pc.Seq([10, 20, 30]).get(5)
        NONE

        ```
        """
        try:
            return Some(self.__getitem__(index))  # pyright: ignore[reportReturnType]
        except IndexError:
            return NONE

    def rev(self) -> Iter[T]:
        """Return an `Iterator` with the elements of the `Sequence` in reverse order.

        Returns:
            Iter[T]: An `Iterator` with the elements in reverse order.

        Example:
        ```python
        >>> import pyochain as pc
        >>> pc.Seq([1, 2, 3]).rev().collect()
        Seq(3, 2, 1)

        ```
        """
        from .._iter import Iter

        return Iter(reversed(self))

    def is_distinct(self) -> bool:
        """Return True if all items of the `Sequence` are distinct.

        Returns:
            bool: True if all items are distinct, False otherwise.

        ```python
        >>> import pyochain as pc
        >>> pc.Seq([1, 2]).is_distinct()
        True

        ```
        """
        return cz.itertoolz.isdistinct(self)

get(index)

get(index: int) -> Option[T]
get(index: slice) -> 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

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:

>>> import pyochain as pc
>>> pc.Seq([10, 20, 30]).get(1)
Some(20)
>>> pc.Seq([10, 20, 30]).get(5)
NONE

Source code in src/pyochain/traits/_iterable.py
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
def get(self, index: int | slice) -> 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): 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
    >>> import pyochain as pc
    >>> pc.Seq([10, 20, 30]).get(1)
    Some(20)
    >>> pc.Seq([10, 20, 30]).get(5)
    NONE

    ```
    """
    try:
        return Some(self.__getitem__(index))  # pyright: ignore[reportReturnType]
    except IndexError:
        return NONE

is_distinct()

Return True if all items of the Sequence are distinct.

Returns:

Name Type Description
bool bool

True if all items are distinct, False otherwise.

>>> import pyochain as pc
>>> pc.Seq([1, 2]).is_distinct()
True
Source code in src/pyochain/traits/_iterable.py
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
def is_distinct(self) -> bool:
    """Return True if all items of the `Sequence` are distinct.

    Returns:
        bool: True if all items are distinct, False otherwise.

    ```python
    >>> import pyochain as pc
    >>> pc.Seq([1, 2]).is_distinct()
    True

    ```
    """
    return cz.itertoolz.isdistinct(self)

rev()

Return an Iterator with the elements of the Sequence in reverse order.

Returns:

Type Description
Iter[T]

Iter[T]: An Iterator with the elements in reverse order.

Example:

>>> import pyochain as pc
>>> pc.Seq([1, 2, 3]).rev().collect()
Seq(3, 2, 1)

Source code in src/pyochain/traits/_iterable.py
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
def rev(self) -> Iter[T]:
    """Return an `Iterator` with the elements of the `Sequence` in reverse order.

    Returns:
        Iter[T]: An `Iterator` with the elements in reverse order.

    Example:
    ```python
    >>> import pyochain as pc
    >>> pc.Seq([1, 2, 3]).rev().collect()
    Seq(3, 2, 1)

    ```
    """
    from .._iter import Iter

    return Iter(reversed(self))