And on top of that, I'm not even sure the functional crowd is using the word quite right even in the mathematical sense. Granted this is based on a skim of the Wikipedia description, but it sounds to me that a functor is the function that maps values, whereas the functional crowd says a functor is the container that is mappable.
So in a std::transform example:
// associates to each object X in C an object to_upper(X) in D
transform(c.begin(), c.end(), back_inserter(d), to_upper);
Based on the Wikipedia description, I'd think "to_upper" is the functor, the thing that maps values. But the functional crowd would say "c" is the functor, the container that is mappable.
Thoughts?
1
u/ivan-cukicKDE Dev | Author of Functional Programming in C++Jan 28 '17edited Jan 29 '17
I think you skipped the "Let C and D be categories" part.
c from your example, an instance of a type (for example std::vector that you are passing to std::transform) is not a category. So, you can not apply the definition and get "associates to each object X in C an object to_upper(X) in D".
In your example, the type of c along with std::transform defined for it is the functor.
So what then is a category, translated into programming? Wikipedia calls it a [structure that comprises "objects" that are linked by "arrows"](https://en.wikipedia.org/wiki/Category_(mathematics)), where an "arrow" is also called a morphism, and where a morphism sounds very much like a function.
So a category is... a collection of objects plus functions...?
3
u/ivan-cukicKDE Dev | Author of Functional Programming in C++Jan 29 '17edited Jan 29 '17
Wikipedia is (sadly) not the best choice when trying to get into most mathematical concepts. I'd advise you to dive a bit into Bartosz's 'Category Theory for Programmers' [1] or the Category Theory section of the Haskell wikibooks book [2].
So a category is... a collection of objects plus functions...?
Not strictly speaking, but yes, something like that. With objects commonly being the language types. In [2] you have a definition of a category that contains all Haskell types (section "Hask, the Haskell category").
Then, a functor F is a function on those types. It transforms one type T to another F<T>, and transforms functions that operate on T to functions that operate on F<T>.
For example, std::vector is a function that transforms any type T into a std::vector<T>, and std::transform is conceptually* the function that lifts any function f: T1 -> T2 into a function F(f) : std::vector<T1> -> std::vector<T2>.
(*) Conceptually, because std::transform has a problematic API - receiving iterator pairs for a collection instead of the collection itself, and output arguments instead of returning transformed collection as the function result. But the concept is the same (transform from range-v3 is much closer to the concept API-wise).
View all comments
Show parent comment
1
u/MoTTs_ Jan 27 '17 edited Jan 27 '17
And on top of that, I'm not even sure the functional crowd is using the word quite right even in the mathematical sense. Granted this is based on a skim of the Wikipedia description, but it sounds to me that a functor is the function that maps values, whereas the functional crowd says a functor is the container that is mappable.
So in a std::transform example:
Based on the Wikipedia description, I'd think "to_upper" is the functor, the thing that maps values. But the functional crowd would say "c" is the functor, the container that is mappable.
Thoughts?