Skip to content

Commit

Permalink
Merge pull request #469 from jwilsson/pre-allocate-slice-in-map
Browse files Browse the repository at this point in the history
Pre-allocate slice in generic Map function
  • Loading branch information
mna authored Feb 29, 2024
2 parents 4cbe087 + ce7330a commit 2095230
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2095230

Please sign in to comment.