Not planned
Description
Proposal Details
Proposal Details
I propose the following:
func Transform[S ~[]E, E any, T any](sl S, f func(E) T) []T {
out := make([]T, len(sl))
for i, t := range sl {
out[i] = f(t)
}
return out
}
Something similar to the method from JS: map.
Using this code, you:
- never forget to pre-allocate the slice
- avoid code deduplication
Sample code:
type user struct {
id int64
}
users := make([]user, 0)
...
sendByIDs(slices.Transform(users, func(u user) int64 {
return u.id
}))
Naming
I believe that the name "map" could be confusing and may be associated with the type.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
seankhliao commentedon Apr 30, 2024
Duplicate of #58881
Map
andFilter
#68065