What go-sugaring is?
This is the package with functions from Python/other language, that you are searching for in Go (Golang)
Disclaimer
Most of the functions from this lib became possible to be written because of generics. So, it is make sense to use the library only if your project is ready to use go >=1.18
Installation and usage
- go get github.com/waclawthedev/go-sugaring
- Call the function. For example:
print(sugar.Contains([]string{"a", "b", "c"}, "a"))
//output: true
Already implemented functions:
- sugar.Contains - check if slice contains element
- sugar.IndexesOf - get the slice with occurrences (indexes) of element in slice
- sugar.Map - Map function. Python analog
- sugar.Filter - Filter function. Python analog
- sugar.Reduce - Reduce function. Python analog
- sugar.Flatten - Transforms matrix to slice
- sugar.Min - min element in slice
- sugar.Max - max element in slice
- sugar.Keys - get keys of map as slice
- sugar.Values - get values of map as slice
Get rid of unnecessary allocations
Hi! I just noticed in you code at line 99 and 107 you perform append operations without previous array allocation. I guess you can allocate slice before loop, because you know the length of map. https://github.com/waclawthedev/go-sugaring/blob/60730179bfd9d6e7aadd49e2cac211709e1883f6/sugar.go#L99