Skip to content

Commit

Permalink
Implement Selection.Map by calling the generic Map
Browse files Browse the repository at this point in the history
Tests pass and verified that benchmarks are basically the same,
including memory allocation.
  • Loading branch information
mna committed Feb 22, 2024
1 parent de2d209 commit 1fad3d4
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions iteration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ func (s *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection {
// element in that selection starting at 0, and a *Selection that contains
// only that element.
func (s *Selection) Map(f func(int, *Selection) string) (result []string) {
for i, n := range s.Nodes {
result = append(result, f(i, newSingleSelection(n, s.document)))
}

return result
return Map(s, f)
}

// Generic version of Selection.Map, allowing any type to be returned.
// 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) {
for i, n := range s.Nodes {
result = append(result, f(i, newSingleSelection(n, s.document)))
}

return result
}
}

0 comments on commit 1fad3d4

Please sign in to comment.