Add chunk
This commit is contained in:
parent
0e5608b3a0
commit
da89e5eb25
3 changed files with 50 additions and 0 deletions
17
src/great_ai/utilities/chunk.py
Normal file
17
src/great_ai/utilities/chunk.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue