6

When facing new programming jargon words, I first try to reason about them from an semantic and etymological standpoint when possible (that is, when they aren't obscure acronyms). For instance, you can get the beginning of a hint of what things like Polymorphism or even Monad are about with the help of a little Greek/Latin. At the very least, once you've learned the concept, the word itself appears to go along with it well. I guess that's part of why we name things names, to make mental representations and associations more fluent.

I found Functor to be a tougher nut to crack. Not so much the C++ meaning -- an object that acts (-or) as a function (funct-), but the various functional meanings (in ML, Haskell) definitely left me puzzled.

From the (mathematics) Functor Wikipedia article, it seems the word was borrowed from linguistics. I think I get what a "function word" or "functor" means in that context - a word that "makes function" as opposed to a word that "makes sense". But I can't really relate that to the notion of Functor in category theory, let alone functional programming. I imagined a Functor to be something that creates functions, or behaves like a function, or short for "functional constructor", but none of those seems to fit...

How do experienced functional programmers reason about this ? Do they just need any label to put in front of a concept and be fine with it ? Generally speaking, isn't it partly why advanced functional programming is hard to grasp for mere mortals compared to, say, OO -- very abstract in that you can't relate it to anything familiar ?

Note that I don't need a definition of Functor, only an explanation that would allow me to relate it to something more tangible, if there is any.

closed as primarily opinion-based by gnat, GlenH7, Doval, amon, Bart van Ingen Schenau Aug 21 '14 at 18:25

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • "avoid asking subjective questions where … every answer is equally valid" (help/dont-ask]). meta.programmers.stackexchange.com/questions/6483/… – gnat Aug 21 '14 at 16:07
  • 2
    First, that term refers to different things in Standard ML/OCaml and Haskell. Second, if you're wanting to learn functional programming concepts, looking at the category theory definition probably won't help you much; at least not without learning the rest of category theory too. And if you do decide to learn category theory, then a functor is whatever category theorists define it to be; attempting to understand it through its etymology or an analogy first will likely lead you astray. – Doval Aug 21 '14 at 16:25
  • 1
    The short answer is that "functor" was coined when a word was needed to describe something like a function but also clearly not a standard mathematical function. It was later repurposed to different contexts for similar reasons, one of which was the context of category theory. In category theory, a function is a specific sort of "arrow" between objects in a category, but a functor is an arrow between categories. So, like a function, but also different. A detailed look at the history I gathered can be found here: pinealservo.com/posts/2014-10-22-ManyFunctionsOfFunctor.html – Levi Pearson Oct 23 '14 at 9:00
  • 1
    @LeviPearson I've just bookmarked that. Most interesting, thanks. Call me a knowledge freak, but for some reason, it's much, much easier for me to grasp a polysemic/ambiguous term in one of its latest usage contexts when I know where it originates :) – guillaume31 Oct 23 '14 at 9:25
  • 1
    This is a great question, and should not have been closed, as it's not subjective -- etymology never is. It's a matter of historical record. Even in cases of competing theories, it's a matter of conflicting evidence rather than subjectivity. – Jonah Mar 20 '16 at 4:52
4

In category theory Functors describe relationships between categories by describing some, or all, mappings from one category to another. There are so many philosophical ideas wrapped up in this (all beautiful) it's difficult to say much that relates this to programming or other real world things that doesn't rob it of some of its intrinsic completeness, but here goes.

Imagine a dataset, and a set of functions that you could perform on that dataset. Now imagine another dataset (maybe looking or feeling very different from the first) and a set of functions you could perform on that dataset. A functor between the two dataset+function 'things' would map the dataset+function thing 1 to dataset+function thing 2 in a meaningful way i.e. would relate functions to functions so as to preserve some properties of the functions between things. Functors therefore express relationships between 'meanings' expressed by the functions in each 'thing'.

It's almost like patterns in programming - the first dataset and function 'thing' might be 'people registered to vote' and functions like 'voting history and likelihood to vote for different parties functions/mappings', the second dataset and function 'thing' might be 'quark groupings in atoms' and functions 'spin interaction prediction under different gravitational influences' and 'total quark breakdown likelihood' or some such weird stuff. A functor would map between one 'world' (dataset and functions on it) and another in a way that allowed you to talk about how you test/express truths about functional outcomes in one world and transfer the results in another because you know how the functions and datasets are related in a specific way.

Functors maps relationships between semantics i.e. are higher-order mechanisms for expressing commonalities between meanings of functional systems.

Sorry, that's as clear as I can express category theoretic functors in a 'real world' summary.

  • As another example you can use lists. You take a type a with operations (functions) on a. Then you take another type, [a] which also has operations. A functor from a to [a] will define (1) a mapping from a to [a] (namely, the one that turns each x into [x]) and (2) a mapping from functions of type a -> a to functions of type [a] -> [a]. So what is the functor doing? It is mapping the structure of type a onto a substructure of type [a]. – Giorgio Aug 21 '14 at 21:11
  • 1
    @DavidScholefield Thanks, that's actually a very good explanation :) So I guess the name comes from the fact that a Functor relates functions to functions? – guillaume31 Aug 22 '14 at 9:00
  • Yes, precisely. The functor defines how the meanings encapsulated in one group of functions relates to another. – David Scholefield Aug 22 '14 at 13:04
  • Wonderful post. I wish I could upvote it again. – Jonah Mar 20 '16 at 4:58
  • even with the examples, it's all still a bit abstract and hard to grasp. I'm still in some sort of stage where I can follow along in the definitions and I could even define a functor of my own given time, but it's not yet internalized so I can reason about them intuitively. answers like this take me one step closer at a time though. I find @Giorgio's list-example very helpful. lists are probably the most intuitive functors, no? – sara Mar 29 '16 at 9:49
6

As far as I know, they are just math words you need to memorize, akin to "hypotenuse." They didn't click for me until I stopped thinking of them as a standalone abstract concept with names that surely make sense to someone, and started thinking of them as badly-named interfaces that types implement.

Without the historical baggage of category theory, programmers would call a Functor a Mappable or something like that. Alas, they didn't ask us, so we're stuck with names that mathematicians made up.

  • 1
    "hypotenuse" - from Greek hypoteinousa "stretching under" (the right angle)... from hypo- "under" + teinein "to stretch". Makes perfect sense :) As does David's answer about functors.... Btw, I still agree the name is poor, but I think the etymology actually gives you deeper insight, so there's value in trying to make sense of it. – Jonah Mar 20 '16 at 5:08
  • "Alas, they didn't ask us, so we're stuck with names that mathematicians made up.": I do not understand why you find mappable more intuitive than functor. Functions are maps and functors are, well, some kind of function (between categories). I would even argue that mappable is misleading: any set is mappable onto another set while a functor does more than just mapping: it respects the underlying structures it is mapping. – Giorgio Mar 30 '16 at 9:23

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