samber / lo

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
https://pkg.go.dev/github.com/samber/lo
MIT License
18.02k stars 830 forks source link

feat: Implement ChunkMap Function (#533) #538

Open oswaldom-code opened 2 months ago

oswaldom-code commented 2 months ago

This PR introduces a new function ChunkMap, which splits a given map into chunks of a specified size. This functionality helps manage larger datasets by allowing developers to process them in smaller, more manageable pieces.

Changes Made:

Added the ChunkMap function to slice.go. Implemented a simple chunking mechanism that creates an array of maps. Added tests to validate the functionality, focusing on the number of chunks produced rather than the order or specific values of the elements. Usage:

result := ChunkMap(map[string]int{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}, 2)

This will return an array of maps, each containing up to 2 key-value pairs from the original map.

Run it on Playground

Reference:

This PR addresses the request made in Issue #533, where the user highlighted the need for a lo.chunk method for maps to simplify batch processing of API requests.