[Compose] 20. Principled type conversions with Natural Transformations
Natural Transformations, let's explain it by using a coding example, for example, we have a 'Either' holding a value 'a' and we want to transform a' Task' that holding an 'a'.
// Either(a) -> Task(a)
Let's start coding:
const Either = require('data.either');
const {Right, Left, fromNullable} = Either;
const Task = require('data.task');
const eitherToTask = e =>
e.fold(Task.rejected, Task.of);
eitherToTask(Right('Good')).fork(
e => console.error('err', e),
x => console.log('res', a)
) // 'res' Good
Let's go thought:
const eitherToTask = e =>
e.fold(Task.rejected, Task.of);
'Either' has 'fold' method (left, right), as we know 'fold' will unbox the contianer just return the value, so that it will unbox the 'Either' type, and we wrap the return value with 'Task'. So now, 'eitherToTask' return a 'Task'
eitherToTask(Right('Good')).fork(...)
The result we using 'Right' instead of 'Left' we will explain later, but here, we know we have a 'Task', therefore we can call 'fork' to trigger the side effect.
Law:
natural_transform(Functor).map(function) === natural_transform(Functor.map(function))
On the left side of equals, we have natural transform function wraps a Functor, then map to a function.
On the right side, we have a natural transform function wrap functor that map to a funciton.
Let's see an example:
const Box = x => ({
map: f => Box(f(x)),
fold: f => f(x)
})
const boxToEither = b =>
b.fold(Right);
const res1 = boxToEither(Box()).map(x => x * );
console.log(res1); // Either (200)
const res2 = boxToEither(Box().map(x => x * ));
console.log(res2); // Either (200)
What if we using 'Left' instead of 'Right':
const Box = x => ({
map: f => Box(f(x)),
fold: f => f(x)
})
const boxToEither = b =>
b.fold(Left);
const res1 = boxToEither(Box()).map(x => x * );
console.log(res1); // Either(100)
const res2 = boxToEither(Box().map(x => x * ));
console.log(res2); // Either(200)
As we can see 'res1' is no longer equals to 'res2', because Left will ingore mapping function.
Another example: List -> Either:
// first :: [] -> Either
const first = xs => fromNullable(xs[]);
const res3 = first([,,]).map(x => x +);
const res4 = first([,,].map(x => x +));
console.log(res3); // Either(2)
console.log(res4); // Either(2)
These two shall be equal and they are. Any function that satisfies this equation is a natural transformation. Let's look at this on the board here.

If we have some F(a) and some functor holding an a and we map(f) over it, it transforms that a to a b. we're just mapping a function from the type a to some type b here all inside our functor f. Then we run a natural transformation we'll have a G(b).
If we take the other path moving downward we'll first naturally transform our functor holding an a into the G(a) here, and then we map(f) over that to get a G(b). We end up with the same result. This can be quite useful.
[Compose] 20. Principled type conversions with Natural Transformations的更多相关文章
- [Compose] 21. Apply Natural Transformations in everyday work
We see three varied examples of where natural transformations come in handy. const Right = x => ( ...
- A Tour of Go Type conversions
The expression T(v) converts the value v to the type T. Some numeric conversions: var i int = 42 var ...
- Type conversions in C++类型转换
###Implicit conversions隐式转换* 可以在基本类型之间自由转换:* 可以把任何类型的pointer转换为void pointer:* 可以将子类pointer转换为基类point ...
- C语言 结构体指针赋值 incompatible types when assigning to type 'char[20]' from type 'char *'
strcpy(pstudent->name, "guo zhao wei "); 为什么错误,该怎么写,(红色行) 追问 为什么不能直接赋值啊, 追答 用char n ...
- 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)
NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.
- C++: Type conversions
1.static_cast static_cast可以转换相关联的类,可以从子类转换成父类.也能从父类转向子类,但是如果转换的父类指针(或者父类引用)所指向的对象是完整的,那么是没有问题:但是 ...
- Type Systems
This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...
- python3.4 data type
#coding=utf-8 #Python 3.4 https://docs.python.org/3.4/library/ #IDE:Eclipse +PyDev Window10 import a ...
- 转载:oracle 自定义类型 type / create type
标签:type create oracle object record 一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarc ...
随机推荐
- iOS开发零基础--Swift篇:逻辑分支
一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...
- QT 应用部署到Android的终端步骤
参考网址: http://blog.csdn.net/syrchina/article/details/17335945
- jsf2.0 tomcat 修改页面后无法立马看到页面修改效果
转载于 http://stackoverflow.com/questions/12203657/jsf2-myfaces-xhtml-modifications-do-not-affect-unti ...
- 以太网客户端提示windows系统自带共享代理解决方法
以太网客户端(Dr.COM)登陆出现windows系统自带共享代理,如下图: 系统win+R调出运行: 在服务中,找到Internet Connection Sharing (ICS): 右键属性,将 ...
- Dynamic CRM 2013学习笔记(四十)流程3 - 对话(Dialog)用法图解
我们将用对话来实现一个简单的满意度调查,下一个问题依赖于上一个问题.对话是同步的,不同于工作流既可以是同步也可以是异步的:对话可以跟用户互动:对话只能手动开始:对话只支持 .Net Framework ...
- 容易答错的JS笔试题
1,考察this var length = 10 function fn(){ alert(this.length) } var obj = { length: 5, meth ...
- C语言 对称矩阵 压缩 实现
对称矩阵压缩的简单实现 (GCC编译). /** * @brief C语言 对称矩阵 压缩 实现 * @author wid * @date 2013-11-03 * * @note 若代码存在 bu ...
- 冲刺阶段 day5
day5 项目进展 今天我们组的成员聚在一起进行了讨论,首先我们继续编写了学生管理这部分的代码,然后负责数据库的同学完成了数据库的部分,最后进行了学生管理这部分的代码复审 存在问题 因为代码不是一天之 ...
- 地址转换协议ARP
地址转换协议ARP 在以太网协议中规定,同一局域网中的一台主机要和另一台主机进行直接通信,必须要知道目标主机的MAC地址.而在TCP/IP协议中,网络层和传输层只关心目标主机的IP地址.这就导致在以太 ...
- [OpenCV] 4、图像叠加 addWeighted
>_<" 这次主要是图像叠加的基本操作: >_<" cv::addWeighted(imageROI, 1.0, logo, 0.6, 0, imageRO ...