Questions tagged [functor]

The term 'functor' has several common meanings: 1. Function object. In object-oriented languages, it is a feature that allows objects to be used as if they were ordinary functions. 2. A mathematical structure which deals with mappings between categories. This concept is a useful abstraction in some programming languages, notably Haskell, where it is implemented as a type class. 3. In OCaml, a module that takes another module as an argument.

In C++ function objects are often called functors.

Function objects can contain their own data values, thus allowing the programmer to emulate closures. Function object can use another function object as a parameter, and can return a structure which encapsulates functions and data specific to a set of inputs, which facilitates the generation of context specific code.

In OCaml, a functor is a module that can take other modules as arguments, producing a specialized module. Effectively a function mapping one module to another.

In Haskell, Functor is a type class that corresponds to the mathematical structure from category theory, Functor, which deals with mappings between categories.

In Prolog, the root of a compound term functor(arg1, arg2, ..., argn) is known as its "functor". Predicate clause's head is represented by a compound term whose functor is the predicate's name (e.g., father(F, S) :- ... .).

References

1606 questions
1061
votes
14 answers

What are C++ functors and their uses?

I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful?
Konrad
  • 39,751
  • 32
  • 78
  • 114
272
votes
7 answers

Function passed as template argument

I'm looking for the rules involving passing C++ templates functions as arguments. This is supported by C++ as shown by an example here: #include void add1(int &v) { v += 1; } void add2(int &v) { v += 2; } template
SPWorley
  • 11,550
  • 9
  • 43
  • 63
243
votes
19 answers

In Functional Programming, what is a functor?

I've come across the term 'Functor' a few times while reading various articles on functional programming, but the authors typically assume the reader already understands the term. Looking around on the web has provided either excessively technical…
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
231
votes
5 answers

Good examples of Not a Functor/Functor/Applicative/Monad?

While explaining to someone what a type class X is I struggle to find good examples of data structures which are exactly X. So, I request examples for: A type constructor which is not a Functor. A type constructor which is a Functor, but not…
Rotsor
  • 13,655
  • 6
  • 43
  • 57
132
votes
4 answers

F# changes to OCaml

F# is derived from OCaml, but what major items are missing or added? Specifically I'm curious as to whether the resources available for learning OCaml are also useful to someone who wants to learn F#.
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
107
votes
1 answer

Why do we have map, fmap and liftM?

map :: (a -> b) -> [a] -> [b] fmap :: Functor f => (a -> b) -> f a -> f b liftM :: Monad m => (a -> b) -> m a -> m b Why do we have three different functions that do essentially the same thing?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
85
votes
5 answers

Monads as adjunctions

I've been reading about monads in category theory. One definition of monads uses a pair of adjoint functors. A monad is defined by a round-trip using those functors. Apparently adjunctions are very important in category theory, but I haven't seen…
Bartosz Milewski
  • 11,012
  • 5
  • 36
  • 45
83
votes
7 answers

Why should I use applicative functors in functional programming?

I'm new to Haskell, and I'm reading about functors and applicative functors. Ok, I understand functors and how I can use them, but I don't understand why applicative functors are useful and how I can use them in Haskell. Can you explain to me with a…
0xAX
  • 20,957
  • 26
  • 117
  • 206
83
votes
11 answers

Why can't I define a function inside another function?

This is not a lambda function question, I know that I can assign a lambda to a variable. What's the point of allowing us to declare, but not define a function inside code? For example: #include int main() { // This is illegal //…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
82
votes
12 answers

Why override operator()?

In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led…
JeffV
  • 52,985
  • 32
  • 103
  • 124
63
votes
5 answers

Why does a js map on an array modify the original array?

I'm quite confused by the behavior of map(). I have an array of objects like this: const products = [{ ..., 'productType' = 'premium', ... }, ...] And I'm passing this array to a function that should return the same array but with all…
Edwin Joassart
  • 930
  • 1
  • 7
  • 19
62
votes
7 answers

Why use functors over functions?

Compare double average = CalculateAverage(values.begin(), values.end()); with double average = std::for_each(values.begin(), values.end(), CalculateAverage()); What are the benefits of using a functor over a function? Isn't the first a lot easier…
DanDan
  • 10,462
  • 8
  • 53
  • 69
62
votes
3 answers

Sets, Functors and Eq confusion

A discussion came up at work recently about Sets, which in Scala support the zip method and how this can lead to bugs, e.g. scala> val words = Set("one", "two", "three") scala> words zip (words map (_.length)) res1: Set[(java.lang.String, Int)] =…
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
60
votes
3 answers

An example of a Foldable which is not a Functor (or not Traversable)?

A Foldable instance is likely to be some sort of container, and so is likely to be a Functor as well. Indeed, this says A Foldable type is also a container (although the class does not technically require Functor, interesting Foldables are all…
Prateek
  • 2,377
  • 17
  • 29
55
votes
2 answers

Fun with repeated fmap

I was playing around with functors, and I noticed something interesting: Trivially, id can be instantiated at the type (a -> b) -> a -> b. With the list functor we have fmap :: (a -> b) -> [a] -> [b], which is the same as map. In the case of the…
hammar
  • 138,522
  • 17
  • 304
  • 385
1
2 3
99 100