In category theory, a functor is what takes you from one category to another. If X and Y are categories, a functor F from X to Y takes each node A of X to a node FA of Y, in such a way that if A -> B then FA -> FB. That is, it preserves arrows!


  • If X is almost any category, it has a "forgetful functor" which copies every node A to the set A of its elements. For example, this lets you treat groups as just the sets of their elements, forgetting the special group structure defined on them. This functor is the basis of rp's assertion in his writeup under category theory that "nodes are sets". They're not, but we can usually think of them as sets by forgetting about their extra properties.
  • If A is a category and A' a pointed category based on A, there is a slightly less forgetful functor from A' to A which takes (A,a) to A (it "forgets" about a).
  • The category Group of groups has a nice functor from Group to Group which takes every group G to a group with exactly the same elements, but the operation takes the elements in opposite order. The groups which this functor takes to themselves are exactly the category of Abelian groups.

If you've just read category theory, you might recognise all of this as being very familiar. And it is! If you look at the Class of all categories (this is a really Big Object, which doesn't even exist in something as puny as Zermelo Fraenkel set theory!), you can create the Category of all categories as follows: every category is a node, and the functors between categories are arrows! This lets you warp your mind with abstract nonsense. Beginners at category theory really like this, as it shows how fundamental category theory is: it even covers itself! It also makes set theorists nervous, as this is exactly the territory the Barber paradox treads on...

In C++ a functor is the name of a special kind of operator overloading. Defining a functor for a class allows you to treat an object as if it were a function. This can be confusing, so is rarely seen. An example would be:

class CSquare
    int operator()( int i )
    { return( i * i ); }

Then, using the above class definition, you could write something like the following:

int main()
    CSquare square;

    int s = square( 5 );

One possible use for functors is for multiple subscript arrays (i.e. matrices). Since overloading the bracket operator only allows a single parameter inside the brackets, you could instead define a functor that taks multiple subscripts.

Although functors have been identified with function objects in print (Coplien?), calling "operator()", or an object that defines it, a "functor" is not universally agreed-upon usage with C++. As far as I know, the term functor has been used in mathematics for probably a century or longer for a mapping that maps functions to other things, for example to numbers. In programming terms, a functor would be a function whose argument type is a function type. Example:
typedef void (function_t)(); // Type of a function taking no args and
                             // returning void

int functor(function_t * const arg); // direct declaration of a function
                                     // that is a functor
Sure, functor may sound cooler than function object or operator(), but using it with this meaning unnecessarily ambiguates it. (Does this make me a pedant? ;-)

"Functor" is also the nickname of the Carnegie Mellon University's School of Computer Science's mascot. Functor is a dragon, and is named in honor of Trogdor the Burninator.

Log in or registerto write something here or to contact authors.