[Compose] 12. Two rules about Funtors

We learn the formal definition of a functor and look at the laws they obey.

Any Functor should follow two rules:

1. Function composition: Map twice equals to call map once with function composition

fx.map(f).map(g) --> fx.map(x => g(f(x)))

Example:

const res1 = Box('answer')
                .map(s => s.substr(3))
                .map(s => s.toUpperCase());

const res2 = Box('answer')
              .map(s => s.substr(3).toUpperCase());

2. Call map with some function on Functor, equals to call function passin functor

id =x => x
fx.map(id) --> id(fx)

Example:

const id = x => x;
const res1 = Box('answer').map(id);

const res2 = id(Box('answer'));
原文地址:https://www.cnblogs.com/Answer1215/p/6198041.html