[Compose] 20. Principled type conversions with Natural Transformations

We learn what a natural transformation is and see the laws it must obey. We will see how a natural transformation must uphold the law of 

nt(x).map(f) == nt(x.map(f))

The rule is:

  "We have functor nt holding x, map to function f"

should equals to:

  "map f to x, then pass into functor nt"

 

Main Goal:

F(a) ---> G(a)

 

For example: 

const boxToEither = b => 
    b.fold(Right)
const res1 = boxToEither(Box(100)).map(x => x * 2);
const res2 = boxToEither(Box(100).map(x => x * 2));

console.log(res1, res2) // Right(200)

 

Here in 'boxToEither', we have to use 'Right' instead of 'Left'. Because using 'Left', it will breaks the rule.

 

你可能感兴趣的:([Compose] 20. Principled type conversions with Natural Transformations)