Skip to content

Range

Bases: PyoSequence[int]


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

                              pyochain.abc._sequences.PyoSequence --> pyochain._range.Range
                                pyochain.abc._collection.PyoCollection --> pyochain.abc._sequences.PyoSequence
                                pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
                                pyochain.rs.Fluent --> pyochain.abc._iterable.PyoIterable
                                pyochain.rs.Pipe --> pyochain.rs.Fluent
                
                pyochain.rs.Tap --> pyochain.rs.Fluent
                

                pyochain.rs.Checkable --> pyochain.abc._iterable.PyoIterable
                

                pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
                
                pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
                

                pyochain.abc._sequences.PyoReversible --> pyochain.abc._sequences.PyoSequence
                



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

A wrapper around the built-in range type that implements the PyoSequence protocol.

start must be specified, unlike the built-in type, but everything else is the same.

Parameters:

Name Type Description Default
start int

The starting value of the range (inclusive).

required
stop int

The ending value of the range (exclusive).

required
step int

The step size between values in the range. Defaults to 1.

1
Example
>>> from pyochain import Range, Dict, Seq
>>>
>>> r = Range(1, 6, 2)
>>> r
Range(1, 6, 2)
>>> r.iter().collect(Seq)
Seq(1, 3, 5)
>>> r.rev().collect(Seq)
Seq(5, 3, 1)
>>> names = ("alice", "bob", "CHARLIE", "dave")
>>> indexed_names = (
...     Range(0, 100)
...     .iter()
...     .zip(names)
...     .map_star(lambda i, n: (i, n.title()))
...     .collect(Dict)
... )
>>> indexed_names
Dict(0: 'Alice', 1: 'Bob', 2: 'Charlie', 3: 'Dave')
Source code in src/pyochain/_range.py
11
12
13
14
15
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Range(PyoSequence[int]):
    """A wrapper around the built-in `range` type that implements the `PyoSequence` protocol.

    `start` must be specified, unlike the built-in type, but everything else is the same.

    Args:
        start (int): The starting value of the range (inclusive).
        stop (int): The ending value of the range (exclusive).
        step (int, optional): The step size between values in the range. Defaults to 1.

    Example:
        ```python
        >>> from pyochain import Range, Dict, Seq
        >>>
        >>> r = Range(1, 6, 2)
        >>> r
        Range(1, 6, 2)
        >>> r.iter().collect(Seq)
        Seq(1, 3, 5)
        >>> r.rev().collect(Seq)
        Seq(5, 3, 1)
        >>> names = ("alice", "bob", "CHARLIE", "dave")
        >>> indexed_names = (
        ...     Range(0, 100)
        ...     .iter()
        ...     .zip(names)
        ...     .map_star(lambda i, n: (i, n.title()))
        ...     .collect(Dict)
        ... )
        >>> indexed_names
        Dict(0: 'Alice', 1: 'Bob', 2: 'Charlie', 3: 'Dave')

        ```
    """

    _inner: Final[range]
    __slots__ = ("_inner",)  # pyright: ignore[reportIncompatibleUnannotatedOverride, reportUnannotatedClassAttribute]

    def __init__(self, start: int, stop: int, step: int = 1) -> None:
        self._inner = range(start, stop, step)

    @override
    def __iter__(self) -> Iterator[int]:
        return iter(self._inner)

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

    @overload
    def __getitem__(self, index: int) -> int: ...
    @overload
    def __getitem__(self, index: slice) -> Sequence[int]: ...
    @override
    def __getitem__(self, index: int | slice[Any, Any, Any]) -> int | Sequence[int]:  # pyright: ignore[reportExplicitAny]
        return self._inner.__getitem__(index)

    @override
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({self._inner.start}, {self._inner.stop}, {self._inner.step})"