|
|
|
|
|
by mseepgood
4537 days ago
|
|
These higher-order functions are a very inefficient way to write loops in Go. When you chain them you will end up with multiple sequential iterations instead of only one. Sort should not allocate and return a new slice, it's better to sort in-place, etc. |
|
Fwiw, slices are inexpensive by design. The elements themselves are not copied. Allocating a new slice is really a small ‘struct’ which serves as a pointer to the underlying array: http://blog.golang.org/go-slices-usage-and-internals
(Gen’ing pointers is supported, which mitigates the potential for allocation.)
It is true that chained operations will be multiple iterations. Whether it will net out to more iterations than otherwise depends on the use.
I’d be interested to see if someone can come up with a pattern where the operations themselves are composed, with a ’.Results()’ method that can intelligently (lazily?) minimize iterations. That’d be impressive.