- Learning Functional Programming in Go
- Lex Sheehan
- 98字
- 2021-07-02 23:13:41
FilterFunc
In the types.go file, we see its definition:
FilterFunc func(string) bool
Looking back at the line in main.go, we see that we use the ByMake filter function:
PrintCars("ByMake - Honda", cars.Filter(ByMake("Honda")))
The ByMake function is defined in the cars.go file:
func ByMake(make string) FilterFunc {
return func(car string) bool {
return s.Contains(car, make)
}
}
The ByMake function is a HOF because it returns a function. Recall that Filter is a HOF because it accepts a function. In this case, ByMake is that function, fn, as we will see in the next section.