def unzip(seq: Iterable) -> Tuple: """ Borrowed from ``toolz.sandbox.core.unzip``, but using cytoolz instead of toolz to avoid the additional dependency. """ seq = iter(seq) # check how many iterators we need try: first = tuple(next(seq)) except StopIteration: return tuple() # and create them niters = len(first) seqs = itertools.tee(itertoolz.cons(first, seq), niters) return tuple(itertools.starmap(itertoolz.pluck, enumerate(seqs)))
def unzip(seq): """ Borrowed from ``toolz.sandbox.core.unzip``, but using cytoolz instead of toolz to avoid the additional dependency. """ seq = iter(seq) # check how many iterators we need try: first = tuple(next(seq)) except StopIteration: return tuple() # and create them niters = len(first) seqs = tee(cons(first, seq), niters) return tuple(starmap(pluck, enumerate(seqs)))
def test_cons(): assert list(cons(1, [2, 3])) == [1, 2, 3]