## Concept:

# Functor

## Headline

A functional programming idiom for mapping over containers

## Illustration

The term "functor" originates from category theory, but this will be of no further concern in this description. In functional programming, "functor" refers to a programming idiom for mapping over contains or compound data. Functors have been popularized by Language:Haskell.

In Haskell, functors are programmed and used with the help of the type class *Functor* which is parametrized by a type constructor for the actual container type:

```
class Functor f
where
fmap :: (a -> b) -> f a -> f b
```

The type constructor parameter *f* is the placeholder for the actual container type. The fmap function (for "functorial map") is the principle operation of a functor: parametrized by a function for mapping container elements of type *a* to elements of type *b*, it provides a mapping at the level of the container types, from *f a* to *f b*. Algebraically, the following properties are required for any functor (given in Haskell notation):

```
fmap id = id
fmap f . fmap g = fmap (f . g)
```

The following *Functor* instance turns lists into a functor:

```
instance Functor []
where
fmap = map
```

Thus, the folklore map function for list processing is a particular example of the notion of functorial map.

Here is another *Functor* instance turning the Maybe type constructor into a functor.

```
instance Functor Maybe
where
fmap _ Nothing = Nothing
fmap f (Just x) = Just (f x)
```

See also the concept of rose trees for more complicated examples of functors.

## Metadata

- Programming idiom
- Vocabulary:Functional programming
- http://www.haskell.org/haskellwiki/Functor
- http://www.haskell.org/haskellwiki/Category_theory/Functor
- http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Functor.html
- http://en.wikipedia.org/wiki/Functor
- Foldable type

### There are no revisions for this page.

## User contributions

## User edits

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**

#### 10 most similar pages:

- Concept:Applicative functor - 1.1221272
- Concept:Rose tree - 0.8555395
- Concept:Monad - 0.6913127
- Concept:Maybe monad - 0.63948333
- Concept:Type class - 0.6216439
- Concept:Maybe type - 0.6148914
- Concept:Higher-order function - 0.5717291
- Concept:Fold_function - 0.54961145
- Concept:Polymorphic type - 0.54497534
- Concept:Map function - 0.52699256

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**