5

In mathematics, adding two functions can be considered an operation in the space of functions. The result is another function.

I can model this by making functions an abstract class and implement all relevant operators for it. An object of that class can be assigned with basic functions (e.g. a constant) and the operators can be used to construct more complex functions. Those objects can be e.g. evaluated or differentiated

What would be a proper name for such an abstract class? Obviously function is a perfectly adequate term in the mathematical sense but ambiguous in programming.

I vaguely remember someone calling it functor but researching that term lead to something different.

17
  • 2
    "but ambiguous in programming" - sure, it might be confusing to some random person who stumbled upon your code, but, if you take the perspective that code is written for to be read by other people, then when choosing names/concepts/etc., take into account who's your intended programmer audience. 1/2 Commented Jun 26, 2022 at 19:57
  • 2
    People who are going to make use of your code will presumably do so precisely because it lets them represent mathematical functions, i.e. they will be interested in working in that domain. They'll think in those terms, they'll read your documentation, etc. They are not going to be confused, and they'll appreciate that you're using terminology that's straightforward to them. So, don't write for the broadest audience imaginable - chose some baseline, and go with that. 2/2 Commented Jun 26, 2022 at 19:57
  • 2
    @HartmutBraun "the name used in computer science for the concept of using functions as objects" - you might be thinking of first-class functions (in OOP at least, since of course a "first-class value" is an object there). But really, a standard name for modelling mathematical functions as objects in a CAS? That's just standard OOP modelling, no specific terminology for that.
    – Bergi
    Commented Jun 26, 2022 at 20:58
  • 4
    I see. I don't think there is. You're representing (modeling) mathematical functions as objects, but that's what objects do - they represent things/concepts, and we name the type to indicate what we intended to model. There's no real reason why modeling functions (as opposed to users, bank accounts, loggers, etc.) would warrant a special name. Commented Jun 26, 2022 at 20:58
  • 4
    @EricDuminil - Check the OPs comment above, it seems that first-class functions are not what the OP is talking about at all. P.S. The fact that we came to the opposite conclusion highlights how it can be hard to communicate ideas :D Commented Jun 26, 2022 at 22:55

3 Answers 3

13

You can take a look at what other people are doing:

I vaguely remember someone calling it functor but researching that term lead to something different.

Remember that a word means only what the person using the word wants it to mean. Nothing more, nothing less.

In this particular case, there are at least three distinct meanings in three distinct contexts, I am aware of:

  • In mathematics, specifically category theory, a functor is (roughly speaking) "something which can be mapped over" or "computation with context". This is also the way the word is used in many programming language communities, for example in Haskell, Scala (e.g. cats and Scalaz), and Python.
  • In ML, functor is the name given a module-level constructor which can construct modules out of other modules. This is similar to a type constructor (aka generic) which can construct types out of other types or a value constructor (aka function) which can construct values out of other values. In other words, in ML, a functor is to modules what a function is to values and a type constructor is to types.
  • In C++, a functor is any object which has an operator(), i.e. any object which can be called as-if it were a function. While this is almost exclusively a C++ usage of the term, this is also the way the term is used in some other programming language communities. Confusingly, this includes communities which also use the term in the first sense, so you really have to look at each individual project and each individual person how they are using the term.

tl;dr summary: you can call it whatever you want, as long as you define what you mean.

4
  • 4
    Impressive list. You may add callable. Worth to mention that even if in the C++ circles we call it a functor, the standard library, still names it a std::function ;-)
    – Christophe
    Commented Jun 26, 2022 at 12:09
  • Impressive list, indeed. My problem was that I couldn’t construct a search string because using the word „function“ didn’t get me anywhere. My second problem is now that, admittedly, I don’t immediately unterstand the documentation provided in the links you listed. On the first read it does not match the concept I have in mind. But I will take a closer look. Commented Jun 26, 2022 at 20:38
  • 1
    Just to emphasize, please don't name something new "functor". It already means six different things, and we don't need someone else fighting for control over that word. If you're not using it in the category theory sense, you're just complicating the word. "Function" and "Callable" are great, and so are more specific things like "Supplier" or "Predicate" (going the Java way) if you know how your function is going to be used. Commented Jun 27, 2022 at 0:39
  • @Christophe:In C++, the abstract class mentioned in the question would would be std::invocable. std::function is a different beast. It's not abstract at all (although it's a template). It performs type erasure; it is a functor itself that can hold many different types of functors and function pointers as long as they can be invoked with roughly the same arguments.
    – MSalters
    Commented Jun 27, 2022 at 11:24
3

It may look pretentious to propose an answer after Jörg's impressive one. But according to your comments, it seems that you're not interested in naming an object that can represent a black-box function ready to be executed, but you are looking at a type that supports symbolic representation of functions and operations to compose functions (f+g, f o g, ...).

In this case, the most general term would be lambda term. It is a concept borrowed from the lambda calculus, i.e. the theory at the foundation of computing. A lambda term can be a variable, a function of a variable (another lambda term associated with a variable, i.e. abstraction), or a function applied to an expression (application). As you can can see, you can operate on and combine lambda terms to construct more complex ones. A more casual alternative would be expression

If this terminology suits your needs (what matters here is the concept -- not the non-inutitive notation that lambda calculus uses) you could consider to the interpreter pattern for the implementation (unless you want to engage in some more complex term rewriting algorithms).

5
  • @EricDuminil Thank you very much! That's what happens when answering in the middle of the night ;-) Corrected.
    – Christophe
    Commented Jun 27, 2022 at 7:28
  • You know, this gave me an idea - maybe for what the OP is doing, the word "term" would suffice - if "expression" is seen as too casual, or potentially unclear in the context of programming? (To me "lambda term" would suggest that the domain / surrounding context has to do with lambda calculus; its use could potentially bring with it the associated semantics/rules.) Commented Jun 27, 2022 at 13:09
  • @FilipMilovanović I agree that “term” sounds less frightening than “lambda term”, and if anyway the lambdas are removed from the term notation, nobody would really miss the word “lambda” ;-)
    – Christophe
    Commented Jun 27, 2022 at 14:56
  • 1
    Thanks Eric and Christophe. You are exactly right, „Lambda“ was too frightening at first glance but it actually seems to represent the concept I was looking for. I practice, however, I think I’d rather use to the term Term as class/interface name. Unfortunately, I can accept only one answer and I gave the credit to Jörg. But also your answer and discussion was very helpful. Commented Jun 27, 2022 at 17:43
  • @HartmutBraun Thanks for the feedback. No problem, I’m not here for the honor but to help. Knowing that this answer helped made my day ;-)
    – Christophe
    Commented Jun 27, 2022 at 18:59
-3

It depends on what you are trying to do. If you are creating a function to wrap other functions in a higher level of abstraction then you might call it a monad .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.