Add chunk

This commit is contained in:
Andras Schmelczer 2022-06-28 18:37:17 +02:00
parent 0e5608b3a0
commit da89e5eb25
3 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,17 @@
from typing import Iterable, List, TypeVar
T = TypeVar("T")
def chunk(values: Iterable[T], chunk_length: int) -> Iterable[T]:
assert chunk_length >= 1
result: List[T] = []
for v in values:
result.append(v)
if len(result) == chunk_length:
yield result
result = []
if len(result) > 0:
yield result