# Functor

### From HaskellWiki

(Difference between revisions)

m (Fixed type mistake in fmap synonym) |
m (fix link) |
||

Line 1: | Line 1: | ||

The <strong><hask>Functor</hask></strong> typeclass represents the mathematical functor: a mapping between categories in the context of category theory. In practice a <hask>functor</hask> represents a type that can be mapped over. | The <strong><hask>Functor</hask></strong> typeclass represents the mathematical functor: a mapping between categories in the context of category theory. In practice a <hask>functor</hask> represents a type that can be mapped over. | ||

− | See also [[ | + | See also [[Applicative functor]] which is a special case of <hask>Functor</hask> |

== Packages == | == Packages == |

## Latest revision as of 22:45, 4 June 2018

The**typeclass represents the mathematical functor: a mapping between categories in the context of category theory. In practice a**

Functor

functor

Functor

## Contents |

## [edit] 1 Packages

- (base) Prelude
- (base) Data.Functor
- (base) Control.Monad

## [edit] 2 Syntax

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

### [edit] 2.1 Minimal Complete Definition

fmap

## [edit] 3 Description

An abstract datatypef a

Functor

Functor

f b

f a

f

f

Functor

f a

a

Functor

fmap

### [edit] 3.1 Functor Laws

- Functors must preserve identity morphisms
fmap id = id

- When performing the mapping operation, if the values in the functor are mapped to themselves, the result will be an unmodified functor.
- Functors preserve composition of morphisms
fmap (f . g) == fmap f . fmap g

- If two sequential mapping operations are performed one after the other using two functions, the result should be the same as a single mapping operation with one function that is equivalent to applying the first function to the result of the second.

fmap

## [edit] 4 Methods

fmap :: (a -> b) -> f a -> f b

- Create a new , from anf busing the results of calling a function on every value in thef a.f a

(<$) :: a -> f b -> f a

- Create a new , from anf aby replacing all of the values in thef bby a given value of typef b.a

## [edit] 5 Related Functions

($>) :: f a -> b -> f b

- Create a new , from anf bby replacing all of the values in thef aby a given value of typef a.b

(<$>) :: (a -> b) -> f a -> f b

- An infix synonym for Data.Functor.fmap

void :: Functor f => f a -> f ()

- create a new from anf ()by replacing all of the values in thef abyf a()