July 2023
Intermediate to advanced
670 pages
17h 13m
English
Functors are the simplest of our trio of type classes, and also have the simplest laws, so we’ll start with them. There are two laws that well-behaved functors should adhere to, and they are both documented in the standard library documentation. There are two functor laws, identity and composition:
| | -- Identity |
| | fmap id = id |
| | -- Composition |
| | fmap (f . g) = fmap f . fmap g |
The identity law of functors is pretty simple: mapping the identity function shouldn’t change the value of the functor. The implication is that fmap should only ever be a mapping function and shouldn’t change anything in the functor except for the value that it’s mapping. The second law, composition, follows naturally from the first. The law of composition ...