Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Go, how can I partition a slice/array/string of any type? [duplicate]

Guava has a generic partition method which can not directly be implemented in Go due to Go's lack of generics. Is there a workaround for this?

like image 654
lf215 Avatar asked Oct 25 '25 04:10

lf215


2 Answers

The gopart library addresses this. It allows partitioning of anything indexable in Go that has any type.

for idxRange := range gopart.Partition(len(bigList), partitionSize) {
        bulkOperation(bigList[idxRange.Low:idxRange.High])
}

Full Executable Example

like image 62
lf215 Avatar answered Oct 27 '25 00:10

lf215


I found this question and answer because I needed the same solution, before I created something that already exists.

I did, however, not like the performance of the solution, so I created a package with a faster and more flexible implementation.

To improve the speed, instead of using channels I used a function that is passed to the function as a callback for the results. I also added implementations using slices and an interface, as well as a channel one to compare the performance of all those methods and give users the flexibility to use the method they require.

To prevent others also need to create their own implementation I made it publicly available at the following url: https://github.com/crosscode-nl/partition

According a benchmark of the code shows that the channel based solution is slower than the func version.

BenchmarkToChan100-4           50000         25862 ns/op
BenchmarkToChan10-4           300000          4939 ns/op
BenchmarkToChan0-4            500000          2727 ns/op
BenchmarkToFunc100-4         5000000           230 ns/op
BenchmarkToFunc10-4         30000000            46.5 ns/op
BenchmarkToFunc0-4          100000000           14.6 ns/op

Example usage:

a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
partition.ToFunc(len(a), 5, func(l int, h int) {
    fmt.Printf("Part: %v\n", a[l:h])
})
// Output:
// Part: [1 2 3 4 5]
// Part: [6 7 8 9]

See benchmarks in repo for details.

like image 44
Patrick Vollebregt Avatar answered Oct 26 '25 23:10

Patrick Vollebregt