Functors in Scala
05 Feb 2017Scala’s type is very rich; even where constructs aren’t well defined you can easily piece together anything that you need. In today’s article, I’ll take you through some different functor types as well as a small primer on variance.
Variance
Type variance is what we use to describe sub-class relationships in a class hierarchy. In scala we use the following notations to denote variances:
Description | Notation | |
---|---|---|
covariant | C[T'] is a subclass of C[T] |
[+T] |
contravariant | C[T] is a subclass of C[T'] |
[-T] |
invariant | C[T] and C[T'] are not related |
[T] |
Functors
Covariant functors are what provide map
or fmap
:
Contravariant functors provide contramap
:
Exponential functors are what provide xmap
:
Applicative functors are what provide apply
and <*>
:
Monads provide bind
, flatMap
or =<<
:
Comonads provide extend
, coflatMap
and <<=
: