4 ms·
An example of something that I couldn’t do with earlier non-generic versions of Go: implement the lodash functions in Go with a nice DX[1]. I kept using []T tho
by sudhirj 5y ago
An example of something that I couldn’t do with earlier non-generic versions of Go: implement the lodash functions in Go with a nice DX[1]. I kept using []T though, and the article has a good example of how to improve that to work with slice based type, not just slices.
So many simple daily use methods like map, forEach, any, all, reduce etc feel much better now.
On the whole generics here seem neat, well thought out, easy to understand and usable, while still allowing you to completely ignore them if you want. Most declarations fit neatly into libraries, with usage often never even having to mention or even know about type declarations (See the examples on the lib). That was a nice change from Java - the inference is pretty nice and neat from what I’ve seen so far.
I’m sure we’ll find it being pushed to its limits, like with ORMs and multi chaining, and that might be ugly, but on the whole I’m very satisfied with this addition.
[1]: https://GitHub.com/sudhirj/slicy https://GitHub.com/sudhirj/slicy
- deleted 5y ago[deleted]
- pkaye 5y agoThere is already an experimental slices package btw. It will be part of the standard packages once the finalize it. https://pkg.go.dev/golang.org/x/exp/slices https://pkg.go.dev/golang.org/x/exp/slices
- sudhirj 5y agoYeah, I’m using and delegating to it when possible. I don’t expect it to be as exhaustive as the lodash set though.
- morelisp 5y agoPlease don't slavishly copy Java/JS idioms. Go is not a language with a JIT and does not have a GC designed to handle lots of garbage; it assumes you won't allocate unless you need to. For example instead of func Drop[T any](array []T, n int) []T { if n > len(array) { n = len(array) } return array[n:] } Consider func Drop[T any](array []T, n int) []T { if n < len(array) { return array[n:] } return nil } Which can collect the underlying array sooner. Instead of your Chunk implementation (which, I want to offer, is better than most), consider an iterative zero-alloc version: func Chunk[T any](array []T, n int) (chunk, remaining []T) { return Take(array, n), Drop(array, n) } (And not performance-related, but instead of indices in `FindIndex` etc., consider returning pointers; they can then generalize more easily to non-slice sequences, or anonymous slices.) (And please don't call slices arrays.)
- sudhirj 5y agoThanks, Will keep this in mind when I do the perf edits and benchmarks. And yeah, didn’t want to confuse slices arrays, but it’s weirder to call them slices.