[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'));

 

posted @ 2016-12-19 16:43  Zhentiw  阅读(183)  评论(0编辑  收藏  举报