
Description
I've been using Go for about 9 months and over that time I've come to like the explicitness of most of the language's semantics. But I feel that the language lacks some convenient ways to handle slices as you will find in most other languages. I feel that adding HOFs such as map/filter/reduce would make code less verbose when handling slices without compromising on explicitness or type-safety.
The current idiom for doing a map operation is something like:
s0 := []int{1,2,3}
s1 := make([]int, 3)
for i := range s0 {
s1[i] = s0[i] * 2
}
While this is a trivial case, and I could have just mutated s0
in this case, it is clearly very imperative and verbose compared to what it could be.
I propose an alternative:
s0 := []int{1,2,3}
s1 := s0.map(func(x int) int {
return x * 2
})
In this case the function passed to map has two properties of interest. The argument which would need to be the slice's Kind (type-checked at compile time), and the return type which could be any type. The one caveat is that the type of the resulting slice will need to be inferred from the type returned from the function.
For example:
s0 := []int{1,2,3}
s1 := s0.map(func(x int) float32 {
return float32(x) / 2
})
where s1
would have type []float32
.
I feel like this seemingly low-hanging fruit would be a step in the right direction in terms of making the language more enjoyable to use with its lack of generics.