Function objects are unrelated to functors in the mathematical field of category theory.
Most modern object-oriented languages such as C++, Java, Python, Ruby and Lisp support the definition of functors and may even make significant use of them.
return (A < B);
}
...
/* Declaration of C sorting function */
void sort_ints(int* begin_items, int num_items, int (*cmpfunc)(int, int) );
...
int main() {
int items[] = {4, 3, 1, 2};
sort_ints(items, sizeof(items)/sizeof(int), compare_function);
}
In C++ a functor may be used instead of an ordinary function by defining a class which overloads the function call operator by defining an operator() member function. In C++ this is called a class type functor, and may appear as follows:
public:
bool operator()(int A, int B) {
return (A < B);
}
};
...
// Declaration of C++ sorting function.
template compare_class functor;
sort_ints(items, sizeof(items)/sizeof(int), functor);
}
Notice that the syntax for providing the callback to the sort_ints() function is identical, but an object is passed instead of a function pointer. When invoked, the callback function is executed just as any other member function, and therefore has full access to the other members (data or functions) of the object.
It is possible to use function objects in situations other than as callback functions (although the shortened term functor is normally not used). Continuing the example,
functor_class Y;
int result = Y(a, b );
In addition to class type functors, other kinds of function objects are also possible in C++. They can take advantage of C++'s member-pointer or template facilities. The expressiveness of templates allows some functional programming techniques to be used, such as defining functors in terms of other functors (like function composition). Much of the C++ Standard Template Library (STL) makes heavy use of template-based function objects.
An advantage of function objects in C++ is performance because unlike a function pointer, a function object can be inlined. For example, consider a simple function which increments its argument implemented as a function object:
std::for_each():
for (; first != last; ++first)
f(*first);
return f;
}
std::for_each() like so:
for_each() will work as expected. The first call will be to this version:
for_each, the compiler will be able to inline the function object because the function is known at compile time whereas within for_each the function cannot be known at compile time and so cannot be inlined.
Actually, a function can easily be known at compile time and the compiler will happily inline it, if it is instructed to. The only requirement is that the compiler has seen the function definition, and that applies equally to functions inside a class or outside. In case we are not inlining however, the linker is instructed to "silently" drop multiple definitions of the same function from different compilation units, without producing an error, but only if said function is a class function. The linker will not dismiss multiple definitions of the same function if it is not a class function.
class countfrom { private:
int count;public: countfrom(int n) : count(n) {} int operator()() { return count++; } };
int main() {
std::generate_n(std::ostream_iterator(std::cout, "n"), 11, countfrom(10));
return 0;}
D provides several ways of declaring functors. Lisp/Python-style using closures or C#-style using delegates, respectively:
void main() {
foreach (straw; haystack ) {
if (needle_test(straw) )
return true;
}
return false;
} int[] haystack = [345,15,457,9,56,123,456];
int needle = 123;
bool needleTest(int n) {
return n == needle;
}
assert(
find(haystack, &needleTest)
);
}
The difference between a delegate and a closure in D is automatically and conservatively determined by the compiler. D also supports function literals, that allow a lambda-style definition:
int[] haystack = [345,15,457,9,56,123,456];
int needle = 123;
assert(
find(haystack, (int n) { return n == needle; })
);
}
In order to allow the compiler to inline the code (see above), functors can also be specified C++-style using operator overloading:
void main() {
foreach (straw; haystack ) {
if (needle_test(straw) )
return true;
}
return false;
} int[] haystack = [345,15,457,9,56,123,456];
int needle = 123;
class NeedleTest {
int needle;
this(int n) { needle = n; }
bool opCall(int n) {
return n == needle;
}
}
assert(
find(haystack, new NeedleTest(needle))
);
}
For an example from Java's standard library, java.util.Collections.sort() takes a List and a functor whose role is to compare objects in the List. But because Java does not have first-class functions, the function is part of the Comparator interface. This could be used as follows.
"10", "1", "20", "11", "21", "12"
});
Collections.sort(list, new Comparator public int compare(String o1, String o2) {
return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
}
});
__call__() method may be called using function-call syntax.An example is this Accumulator class (based on Paul Graham's study on programming language syntax and clarity here):
def __init__(self, n):
self.n = n
def __call__(self, x):
self.n += x
return self.n
An example of this in use (using the interactive interpreter):
>>> a = Accumulator(4)
>>> a(5)
9
>>> a(2)
11
>>> b = Accumulator(42)
>>> b(7)
49
Another way to construct a functor in Python is to use a closure:
def inc(x):
inc.n += x
return inc.n
inc.n = n
return inc
The closure constructor has the syntax Many uses of functors in languages like C++ are simply emulations of the missing closure constructor. Since the programmer cannot directly construct a closure, he or she must define a class which has all of the necessary state variables, and also a member function. Then, construct an instance of that class instead, ensuring that all the member variables are initialized through its constructor. The values are derived precisely from those local variables that ought to be captured directly by a closure. A function-object using the class system, no use of closures:
(defmethod functor-call ((c counter))
(defun make-counter (initial-value)
(defvar *c* (make-counter 10)) (functor-call *c*) --> 11
(functor-call *c*) --> 12
It is this FUNCTOR-CALL generic function which gives us functors, which are a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. We have almost the same syntax: FUNCTOR-CALL instead of FUNCALL. Some Lisps provide "funcallable" objects as a simple extension. Making objects callable using the same syntax as functions is a fairly trivial business. Making a function call operator work with different kinds of "function things", whether they be class objects or closures is no more complicated than making a + operator that works with different kinds of numbers, such as integers, reals or complex numbers. Now, a counter implemented using a closure. This is much more brief and direct. The INITIAL-VALUE argument of the MAKE-COUNTER factory function is captured and used directly. It does not have to be copied into some auxiliary class object through a constructor. It is the counter. An auxiliary object is created, but that happens "behind the scenes". (defvar *c* (make-counter 10)) (funcall *c*) --> 11
(funcall *c*) --> 12
More than one closure can be created in the same lexical environment. A vector of closures, each implementing a specific kind of operation, can quite faithfully emulate an object that has a set of virtual operations. That type of single dispatch object-oriented programming can be done entirely with closures. So there exists a kind of tunnel being dug from both sides of the proverbial mountain. Programmers in OOP languages discover functors by restricting objects to have one "main" function to "do" that object's functional purpose, and even eliminate its name so that it looks like the object is being called! While programmers who use closures are not surprised that an object is called like a function, they discover that multiple closures sharing the same environment can provide a complete set of abstract operations like a virtual table for single dispatch type OOP.
Ruby has a number of objects that can be considered functors, in particular Method and Proc. Ruby also has two kinds of objects that can be thought of as semi-functors: UnboundMethod and block. UnboundMethods must first be bound to an object (thus becoming a Method) before they can be used as a functor. Blocks can be called like functors, but in order to be used in any other capacity as an object (eg. passed as an argument) they must first be converted to a Proc. More recently, symbols (accessed via the literal unary indicator Now, method Because of the variety of forms, the term Functor is not generally used in Ruby to mean a Function object. Rather it has come to represent a type of dispatch delegation introduced by the Ruby Facets project. The most basic definition of which is: This usage is more akin to that used by functional programming languages, like ML, and the original mathematical terminology.
In a more theoretical context a function object may be considered to be any instance of the class of functions, especially in languages such as Common Lisp in which functions are first-class objects. In this case the shortened term functor is rarely used. In Prolog and related languages, functor is a synonym for function symbol.
(lambda (parameters ...) code ...). The (parameters ...) part allows an interface to be declared, so that the function takes the declared parameters. The code ... part consists of expressions that are evaluated when the functor is called. ((value :initarg :value :accessor value-of)))
(incf (value-of c)))
(make-instance 'counter :value initial-value))
;; use the counter: (lambda (incf initial-value)))
;; use the counter Functors in Ruby
:) can also be converted to Procs. Using Ruby's unary & operator—equivalent to calling to_proc on an object, and assuming that method exists—the Ruby Extensions Project created a simple hack. def to_proc
proc { |obj, *args| obj.send(self, *args) }
end
end
foo can be a functor, i.e. a Proc, via &:foo and used via takes_a_functor(&:foo). Symbol.to_proc was officially added to Ruby on June 11, 2006 during RubyKaiga2006. 
def initialize(&func)
@func = func
end
def method_missing(op, *args, &blk)
@func.call(op, *args, &blk)
end
end
Other meanings of functor
In some functional programming languages, such as ML, a functor represents a mapping from modules to modules, and is a technique for reusing code. Functors used in this manner are analogous to the original mathematical meaning of functor in category theory, or to the use of templates in C++. See also
References
(Specifically, chapter 22 is entirely devoted to function objects.) External links