Laws
In order to be Functor, the Functor instance's map function must satisfy two laws. In the rest of this chapter, we will define our laws in terms of an equality: left_expression == right_expression. These equalities must be true for any types and instances specified.
Given a type F that has a Functor[F] instance, for any type A and any instance fa: F[A], the following equalities must be satisfied:
-
Identity preservation: fa.map(identity) == fa. The identity function always returns its argument. Mapping with this function should not change fa.
-
Composition preservation: For any function f and g, fa.map(f).map(g) == fa.map(f andThen g). Mapping f and g successively is the same as mapping with a composition of these functions. This law ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access