Skip to content

Range

Bases: PyoSequence[int]


              flowchart TD
              pyochain.core._range.Range[Range]
              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.PyoSequence --> pyochain.core._range.Range
                                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.core._range.Range href "" "pyochain.core._range.Range"
              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"
            

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

Behaves identically to Python's built-in range type.

Example
from pyochain import Range, Dict, Seq

r = Range(1, 6, 2)
assert r == Range(1, 6, 2)
assert r.iter().collect(Seq) == Seq(1, 3, 5)
assert r.rev().collect(Seq) == Seq(5, 3, 1)
names = ("alice", "bob", "CHARLIE", "dave")
indexed_names = (
    Range(100)
    .iter()
    .zip(names)
    .map_star(lambda i, n: (n.title(), i))
    .collect(Dict)
)
assert indexed_names == Dict(Alice=0, Bob=1, Charlie=2, Dave=3)
Source code in pyochain/core/_range.pyi
 6
 7
 8
 9
10
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
71
72
73
74
75
@final
class Range(PyoSequence[int]):
    """A wrapper around the built-in `range` type that implements the `PyoSequence` protocol.

    Behaves identically to Python's built-in range type.

    Example:
        ```python
        from pyochain import Range, Dict, Seq

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

    @overload
    def __new__(cls, stop: int, /) -> Self: ...
    @overload
    def __new__(cls, start: int, stop: int, step: int = 1, /) -> Self: ...
    def __new__(cls, *args: int) -> Self:
        """Creates a new `Range` instance.

        The number of arguments passed will determine the behavior of the range:

            - 1 => `Range` from 0 to *stop* (exclusive).
            - 2 =>  `Range` from *start* (inclusive) to *stop* (exclusive).
            - 3 => `Range` from *start* to *stop* with *step*
            - 4 or more => Raises a `TypeError`.

        Args:
          *args (int): Start, stop, and step values for the range.

        Returns:
            Self: A new `Range` instance.

        Examples:
            ```python
            from pyochain import Range

            assert tuple(Range(5)) == (0, 1, 2, 3, 4)
            assert tuple(Range(1, 5)) == (1, 2, 3, 4)
            assert tuple(Range(1, 5, 2)) == (1, 3)
            ```

        """
    @override
    def __iter__(self) -> Iterator[int]: ...
    @override
    def __len__(self) -> int: ...
    @overload
    def __getitem__(self, key: SupportsIndex, /) -> int: ...
    @overload
    def __getitem__(self, key: slice[SupportsIndex | None], /) -> Self: ...
    @override
    def __getitem__(
        self, index: SupportsIndex | slice[SupportsIndex | None]
    ) -> int | Self: ...
    @override
    def __reversed__(self) -> Iterator[int]: ...

__new__(*args)

__new__(stop: int) -> Self
__new__(start: int, stop: int, step: int = 1) -> Self

Creates a new Range instance.

The number of arguments passed will determine the behavior of the range:

- 1 => `Range` from 0 to *stop* (exclusive).
- 2 =>  `Range` from *start* (inclusive) to *stop* (exclusive).
- 3 => `Range` from *start* to *stop* with *step*
- 4 or more => Raises a `TypeError`.

Parameters:

Name Type Description Default
*args int

Start, stop, and step values for the range.

()

Returns:

Name Type Description
Self Self

A new Range instance.

Examples:

from pyochain import Range

assert tuple(Range(5)) == (0, 1, 2, 3, 4)
assert tuple(Range(1, 5)) == (1, 2, 3, 4)
assert tuple(Range(1, 5, 2)) == (1, 3)
Source code in pyochain/core/_range.pyi
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
def __new__(cls, *args: int) -> Self:
    """Creates a new `Range` instance.

    The number of arguments passed will determine the behavior of the range:

        - 1 => `Range` from 0 to *stop* (exclusive).
        - 2 =>  `Range` from *start* (inclusive) to *stop* (exclusive).
        - 3 => `Range` from *start* to *stop* with *step*
        - 4 or more => Raises a `TypeError`.

    Args:
      *args (int): Start, stop, and step values for the range.

    Returns:
        Self: A new `Range` instance.

    Examples:
        ```python
        from pyochain import Range

        assert tuple(Range(5)) == (0, 1, 2, 3, 4)
        assert tuple(Range(1, 5)) == (1, 2, 3, 4)
        assert tuple(Range(1, 5, 2)) == (1, 3)
        ```

    """