Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a Predicate and a Functor?

I just read somebody call a class with a constructor and an operator() a predicate:

// Example
class Foo {
  public:
    Foo(Bar);
    bool operator()(Baz);
  private:
    Bar bar;
};

However, I haven't heard the word predicate being used in this context before. I would call such a thing a functor. For me, a predicate would be something from the domain of formal logic.

This raises the following questions:

  • Is this a common word for something like Foo?
  • Are both terms used interchangeably, or do they mean slightly different things?
  • Or
    • Does the return type (bool versus something else) have something to do with it?
    • What about the operator() being const?
like image 996
bitmask Avatar asked Aug 04 '12 03:08

bitmask


People also ask

What is a predicate functor?

A functor (function object) is simply any object that can be called as if it is a function i.e. an object of a class that overload operator() the function call operator. A functor can be considered as a C++ equivalent of function pointers in C.

How do you define a functor?

In functional programming, a functor is a design pattern inspired by the definition from category theory, that allows for a generic type to apply a function inside without changing the structure of the generic type.

What is the difference between a functor and a function pointer?

A function pointer allows a pointer to a function to be passed as a parameter to another function. Function Objects (Functors) - C++ allows the function call operator() to be overloaded, such that an object instantiated from a class can be "called" like a function.

What is a functor in Prolog?

functor, functor In Prolog, the word functor is used to refer to the atom at the start of a structure, along with its arity, that is, the number of arguments it takes. For example, in likes(mary, pizza) , likes/2 is the functor.


2 Answers

Functor is a term that refers to an entity that supports operator () in expressions (with zero or more parameters), i.e. something that syntactically behaves as a function. Functor is not necessarily an object of some class with overloaded operator (). Ordinary function names are functors as well. Although in some contexts you can see the term "functor" used in a more narrow and exclusive sense: just class objects, but not ordinary functions.

A predicate is a specific kind of functor: a functor that evaluates to a boolean value. It is not necessarily a value of bool type, but rather a value of any type with "boolean" semantics. The type should be implicitly convertible to bool though.

like image 97
AnT Avatar answered Oct 24 '22 22:10

AnT


The shown class is a functor that implements a predicate.

A predicate is a boolean function.

About the operator() being non-const here: it should ideally be const, yes.

like image 4
Cheers and hth. - Alf Avatar answered Oct 24 '22 23:10

Cheers and hth. - Alf