Merge pull request #469 from jwilsson/pre-allocate-slice-in-map

Pre-allocate slice in generic Map function
This commit is contained in:
Martin Angers
2024-02-29 09:10:15 -05:00
committed by GitHub
+3 -1
View File
@@ -37,8 +37,10 @@ func (s *Selection) Map(f func(int, *Selection) string) (result []string) {
// Map is the generic version of Selection.Map, allowing any type to be
// returned.
func Map[E any](s *Selection, f func(int, *Selection) E) (result []E) {
result = make([]E, len(s.Nodes))
for i, n := range s.Nodes {
result = append(result, f(i, newSingleSelection(n, s.document)))
result[i] = f(i, newSingleSelection(n, s.document))
}
return result