Skip to content

Iter

Bases: PyoIterator[T], ArgsWrapper[T]


              flowchart TD
              pyochain.core._iterators.Iter[Iter]
              pyochain.abc._iterator.PyoIterator[PyoIterator]
              pyochain.abc._iterable.PyoIterable[PyoIterable]
              pyochain.abc._mixins.Checkable[Checkable]
              pyochain.abc._mixins.Fluent[Fluent]
              pyochain.abc._mixins.Pipe[Pipe]
              pyochain.abc._mixins.Tap[Tap]
              pyochain.abc.constructors.ArgsWrapper[ArgsWrapper]
              pyochain.abc.constructors.FromArgs[FromArgs]
              pyochain.abc.constructors.FromIter[FromIter]
              pyochain.abc.constructors.Wrapper[Wrapper]

                              pyochain.abc._iterator.PyoIterator --> pyochain.core._iterators.Iter
                                pyochain.abc._iterable.PyoIterable --> pyochain.abc._iterator.PyoIterator
                                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.constructors.ArgsWrapper --> pyochain.core._iterators.Iter
                                pyochain.abc.constructors.FromArgs --> pyochain.abc.constructors.ArgsWrapper
                                pyochain.abc.constructors.FromIter --> pyochain.abc.constructors.FromArgs
                

                pyochain.abc.constructors.Wrapper --> pyochain.abc.constructors.ArgsWrapper
                



              click pyochain.core._iterators.Iter href "" "pyochain.core._iterators.Iter"
              click pyochain.abc._iterator.PyoIterator href "" "pyochain.abc._iterator.PyoIterator"
              click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
              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"
              click pyochain.abc.constructors.ArgsWrapper href "" "pyochain.abc.constructors.ArgsWrapper"
              click pyochain.abc.constructors.FromArgs href "" "pyochain.abc.constructors.FromArgs"
              click pyochain.abc.constructors.FromIter href "" "pyochain.abc.constructors.FromIter"
              click pyochain.abc.constructors.Wrapper href "" "pyochain.abc.constructors.Wrapper"
            

Concrete implementation for abc::PyoIterator.

Can be instantiated from any Iterable (like lists, sets, generators, etc.) efficiently (it only calls the builtin iter() on the input).

As such, creating an Iter from an Iterator is virtually free.

Tip

Iter::__iter__() returns the underlying wrapped Iterator, hence native speed is kept.

i.e Iter(...).map(f).collect(list) is as fast as list(map(f, [...])).

See Also

abc::PyoIterator: The abstract base class that Iter implements.

Example
from pyochain import Iter, Seq

data = (0, 1, 2, 3, 4)

assert Iter(data).collect(Seq) == Seq(data)
iterator = Iter(data)

# First we have a tuple iterator
assert iterator.__iter__().__class__.__name__ == "tuple_iterator"

# Now we have a map object
mapped = iterator.map(lambda x: x * 2)
assert mapped.__iter__().__class__.__name__ == "map"

# We collect it, by default into a Seq
assert mapped.collect(Seq) == Seq(0, 2, 4, 6, 8)

# iterator is now exhausted
assert iterator.collect(Seq) == Seq()
Source code in pyochain/core/_iterators.pyi
  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
 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
@final
class Iter[T](PyoIterator[T], ArgsWrapper[T]):
    """Concrete implementation for `abc::PyoIterator`.

    Can be instantiated from any `Iterable` (like lists, sets, generators, etc.) efficiently (it only calls the builtin `iter()` on the input).

    As such, creating an `Iter` from an `Iterator` is virtually free.

    Tip:
        `Iter::__iter__()` returns the underlying wrapped `Iterator`, hence native speed is kept.

        i.e `Iter(...).map(f).collect(list)` is as fast as `list(map(f, [...]))`.

    See Also:
        [`abc::PyoIterator`][PyoIterator]: The abstract base class that `Iter` implements.

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

        data = (0, 1, 2, 3, 4)

        assert Iter(data).collect(Seq) == Seq(data)
        iterator = Iter(data)

        # First we have a tuple iterator
        assert iterator.__iter__().__class__.__name__ == "tuple_iterator"

        # Now we have a map object
        mapped = iterator.map(lambda x: x * 2)
        assert mapped.__iter__().__class__.__name__ == "map"

        # We collect it, by default into a Seq
        assert mapped.collect(Seq) == Seq(0, 2, 4, 6, 8)

        # iterator is now exhausted
        assert iterator.collect(Seq) == Seq()
        ```
    """

    def __new__(cls, data: Iterable[T] | T = (), /, *more: T) -> Self:
        """Create a new `Iter` instance.

        If no arguments are provided, an empty `Iterator` is created.

        Args:
            data (Iterable[T] | T): Input data to create the `Iter` instance from.
            *more (T): Additional elements to yield from the iterator.

        Returns:
            Self: A new `Iter` instance.

        Example:
            ```python
            from pyochain import Iter, Range

            data = (0, 1, 2, 3)

            # Create an `Iter` from an iterable
            assert Iter(data).collect(tuple) == Iter(Range(0, 4)).collect(tuple) == data

            # Create an `Iter` from individual elements
            assert Iter(0, 1, 2, 3).collect(tuple) == Iter(*data).collect(tuple) == data

            # Create an empty `Iter`
            assert 0 == Iter().count() == Iter(()).count() == Iter([]).count()
            ```
            You can also easily create an `Iter` from a generator expression:
            ```python
            from pyochain import Iter, Seq

            gen_expr = (x * x for x in range(5))
            assert Iter(gen_expr).collect(Seq) == Seq(0, 1, 4, 9, 16)
            ```
            Or from a generator function:
            ```python
            from pyochain import Iter

            def gen_func():
                for x in range(5):
                    yield x * x

            assert Iter(gen_func()).collect(Seq) == Seq(0, 1, 4, 9, 16)
            ```
        """
    @override
    def __iter__(self) -> Iterator[T]: ...
    @override
    def __next__(self) -> T: ...
    @override
    @staticmethod
    def from_iter[I](iterable: Iterable[I], /) -> Iter[I]: ...
    @override
    @staticmethod
    def of[I](*elements: I) -> Iter[I]: ...
    @override
    @staticmethod
    def wrap[W](wrapped: Iterator[W], /) -> Iter[W]: ...  # pyright: ignore[reportIncompatibleMethodOverride]

__new__(data=(), /, *more)

Create a new Iter instance.

If no arguments are provided, an empty Iterator is created.

Parameters:

Name Type Description Default
data Iterable[T] | T

Input data to create the Iter instance from.

()
*more T

Additional elements to yield from the iterator.

()

Returns:

Name Type Description
Self Self

A new Iter instance.

Example

from pyochain import Iter, Range

data = (0, 1, 2, 3)

# Create an `Iter` from an iterable
assert Iter(data).collect(tuple) == Iter(Range(0, 4)).collect(tuple) == data

# Create an `Iter` from individual elements
assert Iter(0, 1, 2, 3).collect(tuple) == Iter(*data).collect(tuple) == data

# Create an empty `Iter`
assert 0 == Iter().count() == Iter(()).count() == Iter([]).count()
You can also easily create an Iter from a generator expression:
from pyochain import Iter, Seq

gen_expr = (x * x for x in range(5))
assert Iter(gen_expr).collect(Seq) == Seq(0, 1, 4, 9, 16)
Or from a generator function:
from pyochain import Iter

def gen_func():
    for x in range(5):
        yield x * x

assert Iter(gen_func()).collect(Seq) == Seq(0, 1, 4, 9, 16)

Source code in pyochain/core/_iterators.pyi
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
def __new__(cls, data: Iterable[T] | T = (), /, *more: T) -> Self:
    """Create a new `Iter` instance.

    If no arguments are provided, an empty `Iterator` is created.

    Args:
        data (Iterable[T] | T): Input data to create the `Iter` instance from.
        *more (T): Additional elements to yield from the iterator.

    Returns:
        Self: A new `Iter` instance.

    Example:
        ```python
        from pyochain import Iter, Range

        data = (0, 1, 2, 3)

        # Create an `Iter` from an iterable
        assert Iter(data).collect(tuple) == Iter(Range(0, 4)).collect(tuple) == data

        # Create an `Iter` from individual elements
        assert Iter(0, 1, 2, 3).collect(tuple) == Iter(*data).collect(tuple) == data

        # Create an empty `Iter`
        assert 0 == Iter().count() == Iter(()).count() == Iter([]).count()
        ```
        You can also easily create an `Iter` from a generator expression:
        ```python
        from pyochain import Iter, Seq

        gen_expr = (x * x for x in range(5))
        assert Iter(gen_expr).collect(Seq) == Seq(0, 1, 4, 9, 16)
        ```
        Or from a generator function:
        ```python
        from pyochain import Iter

        def gen_func():
            for x in range(5):
                yield x * x

        assert Iter(gen_func()).collect(Seq) == Seq(0, 1, 4, 9, 16)
        ```
    """