mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
Implement Selection.Map by calling the generic Map
Tests pass and verified that benchmarks are basically the same, including memory allocation.
This commit is contained in:
+4
-7
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user