v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/slice/chunk.go (about)

     1  package slice
     2  
     3  func Chunk[T any](array []T, size int) [][]T {
     4  	if size < 1 {
     5  		return nil
     6  	}
     7  	chunks := make([][]T, 0, (len(array)/size)+1)
     8  	for i := 0; i < len(array); i += size {
     9  		end := i + size
    10  		if end > len(array) {
    11  			end = len(array)
    12  		}
    13  		chunks = append(chunks, array[i:end])
    14  	}
    15  	return chunks
    16  }