Skip to content

Seq

Bases: PyoSequence[T]

Represent an in memory Sequence.

Implements the Sequence Protocol from collections.abc.

Provides a subset of Iter methods with eager evaluation, and is the return type of Iter.collect().

The underlying data structure is an immutable tuple, hence the memory efficiency is better than a Vec.

Tip

Seq(tuple) is preferred over Seq(list) as this is a no-copy operation (Python optimizes tuple creation from another tuple). If you have an existing list, consider using Vec.from_ref() instead to avoid unnecessary copying.

Parameters:

Name Type Description Default
data Iterable[T]

The data to initialize the Seq with.

required
Source code in src/pyochain/_iter.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
class Seq[T](PyoSequence[T]):
    """Represent an in memory `Sequence`.

    Implements the `Sequence` Protocol from `collections.abc`.

    Provides a subset of `Iter` methods with eager evaluation, and is the return type of `Iter.collect()`.

    The underlying data structure is an immutable `tuple`, hence the memory efficiency is better than a `Vec`.

    Tip:
        `Seq(tuple)` is preferred over `Seq(list)` as this is a no-copy operation (Python optimizes `tuple` creation from another `tuple`).
        If you have an existing `list`, consider using `Vec.from_ref()` instead to avoid unnecessary copying.

    Args:
            data (Iterable[T]): The data to initialize the Seq with.
    """

    __slots__ = ("_inner",)
    _inner: tuple[T, ...]

    def __init__(self, data: Iterable[T]) -> None:
        self._inner = tuple(data)

    def __iter__(self) -> Iterator[T]:
        return iter(self._inner)

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({_get_repr(self._inner)})"

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

    @overload
    def __getitem__(self, index: int) -> T: ...
    @overload
    def __getitem__(self, index: slice) -> Sequence[T]: ...
    def __getitem__(self, index: int | slice[Any, Any, Any]) -> T | Sequence[T]:
        return self._inner.__getitem__(index)