The blog post introducing range over function types demonstrates an adapter use case.
return func(yield func(V) bool) {
for v := range s {
if f(v) {
if !yield(v) {
return
}
}
}
}
}
and demonstrates Passing a function to a push iterator,
func PrintAllElements[E comparable](s *Set[E]) {
s.All()(func(v E) bool {
fmt.Println(v)
return true
})
}
There’s no particular reason to write this code this way.
The readability of the functional approach seems proportional to the readability of the implicit yield function. But an adapter already has an explicit yield function, and so could be (arguably) simpler:
func Filter[V any](f func(V) bool, s iter.Seq[V]) iter.Seq[V] {
return func(yield func(V) bool) {
s(func(v V) bool { return !f(v) || yield(v) })
}
}
The compiler appears to produce identical output, so it's merely a style question.