[Compose] 8. A curated collection of Monoids and their uses
const { List } = require('immutable-ext');
const Right = x => ({
chain : f => f(x),
ap : other => other.map(x),
traverse : (of, f) => f(x).map(Right),
map : f => Right(f(x)),
fold : (f, g) => g(x),
concat : o => o.fold(_ => Right(x), y => Right(x.concat(y))),
toString : () => `Right(${x})`
})
const Left = x => ({
chain : f => Left(x),
ap : other => Left(x),
traverse : (of, f) => of(Left(x)),
map : f => Left(x),
fold : (f, g) => f(x),
concat : o => o.fold(_ => Left(x), y => o),
toString : () => `Left(${x})`
});
const fromNullable = x => x != null ?
Right(x) :
Left(null);
const tryCatch = f => {
try {
return Right(f())
}
catch( e ) {
return Left(e)
}
};
let stats = List.of({
page : 'Home',
view :
}, {
page : 'About',
view :
}, {
page : 'Help',
view :
});
const Sum = x => ({
x,
concat : ({ x: y }) => Sum(x + y),
toString : () => `Sum(${x})`
});
Sum.empty = () => Sum();
const res = stats.foldMap(x => fromNullable(x.view)
.map(Sum), Right(Sum()));
console.log(res.toString()); // Right(Sum(54))
If change the data a litte bit:
let stats = List.of({
page : 'Home',
view :
}, {
page : 'About',
view :
}, {
page : 'Help',
view : null
});
const Sum = x => ({
x,
concat : ({ x: y }) => Sum(x + y),
toString : () => `Sum(${x})`
});
Sum.empty = () => Sum();
const res = stats.foldMap(x => fromNullable(x.view)
.map(Sum), Right(Sum()));
console.log(res.toString()); // Right(Sum(50))
Because the view: null, then it will skip .map(sum).
[Compose] 8. A curated collection of Monoids and their uses的更多相关文章
- [JS Compose] 7. Ensure failsafe combination using monoids
monoids is a semi-group with a neutral element. A semigroup, it does not have an element to return s ...
- DotNet 资源大全中文版(Awesome最新版)
Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...
- Frontend Development
原文链接: https://github.com/dypsilon/frontend-dev-bookmarks Frontend Development Looking for something ...
- 【资源大全】.NET资源大全中文版(Awesome最新版)
算法与数据结构(Algorithms and Data structures) 应用程序接口(API) 应用程序框架(Application Frameworks) 模板引擎(Application ...
- nlp-roadmap
nlp-roadmap https://github.com/graykode/nlp-roadmap nlp-roadmap is Natural Language Processing ROADM ...
- 集合框架学习之Guava Collection
开源工具包: Guava : Google Collection Apache:Commons Collecton 1.1 Google Collections Guava:google的工程师利用传 ...
- Docker Compose 一键部署Nginx代理Tomcat集群
Docker Compose 一键部署Nginx代理Tomcat集群 目录结构 [root@localhost ~]# tree compose_nginx_tomcat/ compose_nginx ...
- (转)A curated list of Artificial Intelligence (AI) courses, books, video lectures and papers
A curated list of Artificial Intelligence (AI) courses, books, video lectures and papers. Updated 20 ...
- [Transducer] Make Transducer works for Iteratable collection and Object
We've seen how we can transduce from arrays or other iterables, but plain objects aren't iterable in ...
随机推荐
- javascript类型系统之基本数据类型与包装类型
javascript的数据类型可以分为两种:原始类型和引用类型 原始类型也称为基本类型或简单类型,因为其占据空间固定,是简单的数据段,为了便于提升变量查询速度,将其存储在栈(stack)中(按值访问) ...
- AJAX - 封装的传参改为传入对象 XML JSON 数据格式
Ajax封装函数,上次是直接传参,这次在原来的基础上改进,模仿jQuery 直接传入对象,把之前的参数都变为这个对象的属性. 这样可以随意调换传入数据的次序. 其他优点? 需要再复习一下. Ajax处 ...
- 内核中的宏定义__init、__initdata和__exit、__exitdata
__init.__initdata和__exit.__exitdata的定义位于<kernel/include/linux/init.h> /* These are for everybo ...
- CISP/CISA 每日一题 13
监控信息系统人员所提供服务的效率和效果的工具: 1.例外报告:识别所有没有成功完成的或出了故障的应用 2.作业重运行报告:大多数异常终止作业都会导致重起 3.操作员问题报告:记录计算机运行问题及解决方 ...
- SSO单点登录学习总结(2)——基于Cookie+fliter单点登录实例
1.使用Cookie解决单点登录 技术点: 1.设置Cookie的路径为setPath("/").即Tomcat的目录下都有效 2.设置Cookie的域setDomain(&quo ...
- Android-ViewPager+Fragment数据更新问题
由于FragmentPagerAdapter内部存在缓存.因此调用notifyDataSetChanged()并不可以去更新Fragment的内容. 參考:http://www.devba.com/i ...
- 81.内存模式实现cgi查询
创建全局的二级指针 char ** g_pp;//全局的二级指针 获取数据有多少行 //获取行数 int getimax() { ; FILE *pf = fopen(path, "r&qu ...
- HDOJ 5409 CRB and Graph 无向图缩块
无向图缩块后,以n所在的块为根节点,dp找每块中的最大值. 对于每一个桥的答案为两块中的较小的最大值和较小的最大值加1 CRB and Graph Time Limit: 8000/4000 MS ( ...
- stm32单片机的封装
接着去查看VREF...
- Web安全之Cookie劫持
1. Cookie是什么? 2. 窃取的原理是什么? 3. 系统如何防Cookie劫持呢? 看完这三个回答, 你就明白哪位传奇大侠是如何成功的!!! Cookie: HTTP天然是无状态的协议, 为了 ...