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?
2 Answers
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.

- 41
- 4
-
Please describe in what way this solution has better performance so I can select it as the preferred answer. – lf215 Mar 06 '19 at 17:28
-
Welcome to StackOverflow! Can you explain in few words what exactly you did? It would be better with excerpts of your code – Alex Yu Mar 06 '19 at 17:29