|
|
|
|
|
by jakewins
3382 days ago
|
|
Go can't do this, because you can't define `map` as returning "a collection of whatever `f` returns". If they build a collections API like this now, without generics, and they then add generic types later, they will be in the same position Java found itself in - lots of painful API migrations. While the generics debate is legendary in go, it does still seem very much like the intention is to eventually include it. Given that, I think it makes perfect sense to wait to introduce more special cases (like `append`). In the meantime: func Map(in []interface{}, f func(interface{}) interface{}) []interface{} {
out := make([]interface{}, len(in))
for i, v := range in {
out[i] = f(v)
}
return out
}
:) |
|
The fact that append exists and the language would be extremely convoluted and inefficient to use is a testimony to how useful generic programming is.