Advertisement
HasteBin0

Python give_chain_link_pairs() — efficient loopback iterator!

Jul 22nd, 2023
1,637
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | Source Code | 0 0
  1. from typing import TypeVar, Iterable, Iterator, Tuple
  2. from more_itertools import spy
  3. from itertools import chain, pairwise
  4.  
  5.  
  6. _T = TypeVar('_T')
  7.  
  8.  
  9. def give_chain_link_pairs(links: Iterable[_T], cycle_preview: int = 1) -> Iterator[Tuple[_T]]:
  10.     first_values, new_iterator = spy(iter(links), n = cycle_preview)
  11.     yield from pairwise(chain(new_iterator, reversed(first_values)))  # loopback
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement