A functor is a structure-preserving transformation between categories.
It
- map objects from one category to objects of another
- while also preserving the arrows between objects
Sort of like homomorphisms between categories.
In FP language like Haskell, the definition of Functor class has two parts:
class Functor f where
fmap :: (a -> b) -> (f a -> f b)
The type f
is what maps objects (Haskell types) to other objects in the same category (Haskell types). It maps a
to f a
.
A monoid (semigroups with identity)
- A set,
S
- combined with an operation,
• : S × S → S
- and an element of
S
, e : 1 → S
Defined in Haskell as following
class Semigroup s => Monoid s where
mempty :: s
mappend :: s -> s -> s
mappend = (<>)
They are two different things but
They are often mentioned together but I haven't been able to quite
connect the dots.
they are often mentioned together because of one another thing, a Monad.
A monad (special cases of monoids or monoids among endofunctors) is
- An endofunctor,
T : S → S
(An endofunctor is simply a functor from a category to itself)
- along with a natural transformation,
μ : S × S → S
, where ×
means functor composition (join
)
- and
η : I → S
, where I
is the identity endofunctor on S
(return
)
which essentially combines those two concepts.
In Haskell
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
implies
fmap f m = m >>= return . f
More succinctly put,
A monad is just a monoid in the category of endofunctors, what's the problem?
which you might have seen jokingly used all over FP forums. It's a version of
a monad in X is just a monoid in the category of endofunctors of X
originally from Mac Lane's Categories for the Working Mathematician.