pyiter.merging_with_next

 1from typing import Callable, Iterable, Iterator
 2from .transform import Transform, T, U
 3
 4
 5class MergingWithNextTransform(Transform[T, U]):
 6    """
 7    Transforms an iterable by applying a function to each element and the next element.
 8    """
 9
10    def __init__(self, iter: Iterable[T], transform: Callable[[T, T], U] = lambda a, b: (a, b)):
11        super().__init__(iter)
12        self.transform = transform
13
14    def __do_iter__(self) -> Iterator[U]:
15        it = iter(self.iter)
16        try:
17            c = next(it)
18            while True:
19                n = next(it)
20                yield self.transform(c, n)
21                c = n
22        except StopIteration:
23            pass
class MergingWithNextTransform(pyiter.transform.Transform[~T, ~U]):
 6class MergingWithNextTransform(Transform[T, U]):
 7    """
 8    Transforms an iterable by applying a function to each element and the next element.
 9    """
10
11    def __init__(self, iter: Iterable[T], transform: Callable[[T, T], U] = lambda a, b: (a, b)):
12        super().__init__(iter)
13        self.transform = transform
14
15    def __do_iter__(self) -> Iterator[U]:
16        it = iter(self.iter)
17        try:
18            c = next(it)
19            while True:
20                n = next(it)
21                yield self.transform(c, n)
22                c = n
23        except StopIteration:
24            pass

Transforms an iterable by applying a function to each element and the next element.

transform