Documentation
¶
Overview ¶
Package chunk provides a faithful port of lodash's `chunk` utility using only the Go standard library. In JavaScript, `_.chunk(array, size)` breaks an array into a list of smaller arrays each holding at most `size` elements; this package exposes the same behavior through the generic Chunk function so it works for slices of any element type.
You reach for Chunk whenever a flat slice needs to be processed or displayed in fixed-size batches: paginating a result set, rendering a grid with a set number of columns per row, sending records to an API that caps how many can be submitted per request, or bounding how much work a single goroutine picks up. It keeps the batching arithmetic out of the caller so the surrounding code can focus on what each group does.
The algorithm mirrors lodash exactly. It first computes the number of groups as the ceiling of len(s) divided by size, preallocates the outer slice to that capacity, then walks the input in strides of size. Each stride is copied into a freshly allocated group slice, so the returned groups never alias the backing array of the input; mutating a returned group leaves the caller's original slice untouched. Every group holds exactly size elements except possibly the last, which holds the remainder when len(s) is not evenly divisible by size.
The edge cases follow lodash's semantics. A size of zero or any negative value is treated as invalid and yields an empty (non-nil) [][]T rather than an error or a panic. An empty or nil input slice likewise yields an empty outer slice. When size is greater than or equal to len(s) the result is a single group containing a copy of every element. The function never returns nil and never mutates its input.
Compared to the Node original, the behavioral parity is intentionally close: invalid sizes collapse to an empty result and uneven splits leave a shorter final group, exactly as in lodash. The one deliberate difference is that Go's static typing replaces lodash's dynamic arrays with a type parameter, so Chunk is compile-time type safe and requires no per-element boxing or runtime type assertions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
Chunk splits s into groups of at most size elements. The final group holds the remaining elements when len(s) is not evenly divisible by size.
If size is less than or equal to zero, Chunk returns an empty slice. The returned chunks reference freshly allocated slices; the input is not mutated.
Example ¶
ExampleChunk splits a flat slice into groups of at most the given size, mirroring lodash's chunk. Every group holds exactly size elements except possibly the last, which holds the remainder when the length is not evenly divisible. Here five elements split into two pairs and a trailing single. The returned groups are freshly allocated copies, so mutating them never touches the input.
package main
import (
"fmt"
"github.com/malcolmston/express/chunk"
)
func main() {
groups := chunk.Chunk([]int{1, 2, 3, 4, 5}, 2)
fmt.Println(groups)
}
Output: [[1 2] [3 4] [5]]
Example (InvalidSize) ¶
ExampleChunk_invalidSize demonstrates the edge-case semantics. A size of zero or any negative value is treated as invalid and yields an empty, non-nil result rather than an error or a panic. The input slice is never mutated and the function never returns nil. This matches lodash, which collapses invalid sizes to an empty array.
package main
import (
"fmt"
"github.com/malcolmston/express/chunk"
)
func main() {
groups := chunk.Chunk([]int{1, 2, 3}, 0)
fmt.Println(len(groups))
}
Output: 0
Example (Strings) ¶
ExampleChunk_strings shows that Chunk is generic and works for a slice of any element type, here strings. It is useful for batching work such as paginating results or capping how many records are sent per request. A size greater than or equal to the length yields a single group containing every element. This call batches four names two at a time.
package main
import (
"fmt"
"github.com/malcolmston/express/chunk"
)
func main() {
groups := chunk.Chunk([]string{"a", "b", "c", "d"}, 2)
fmt.Println(groups)
}
Output: [[a b] [c d]]
Types ¶
This section is empty.