How are functors useful?

With a function you can only do one thing, call it. With a functor you can define additional methods to expose other information and functionality, but still have the convenience of a function-call API.

For example, there are various packages (Polynomials, ApproxFun, etc.) that provide a data structure wrapping a polynomial p. It is natural to make these callable so that you can use the syntax p(x) to evaluate the polynomial, but that’s not all you want to do with polynomials so you need a struct and not an anonymous function.

32 Likes