[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 ...
随机推荐
- IIS进程回收 空闲时Net线程未运行
最近手上的项目,用的是asp.net mvc,后台有个线程在循环接收socket数据,本身在系统运行的时候访问页面没问题,但是发现没访问时,后台没有接收数据,后来知道了是IIS把线程回收了.解决方法如 ...
- BZOJ2780: [Spoj]8093 Sevenk Love Oimaster(广义后缀自动机,Parent树,Dfs序)
Description Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXu ...
- python运算符优先级表
运算符 描述 lambda Lambda表达式 or 布尔“或” and 布尔“与” not x 布尔“非” in,not in 成员测试 is,is not 同一性测试 <,<=,> ...
- http 500 Internal Server Error的错误 ajax请求SpringMVC后台中返回500 Internal Server Error
使用httprequester接口测试能返回数据,但是用ajax返回json格式的时候返回报500Internal Server Error. The server encountered an in ...
- IDFA和IMEI
这里有一些解释: https://www.zhihu.com/question/38856446
- 对象的序列化与反序列化---IO学习笔记(四)
对象的序列化,反序列化 对象的序列化: 就是将Object转换成byte序列 对象的反序列化: 将byte序列转换成Object 序列化流.反序列化流 序列化流(ObjectOutputStream) ...
- OC文件操作、获取文件属性
#import <Foundation/Foundation.h> //获取文件的属性 int main(int argc, const char * argv[]) { @autorel ...
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第六篇:拦截器
摘要 本文将对“MVC公告发布系统”的发布公告功能添加日志功能和异常处理功能,借此来讨论ASP.NET MVC中拦截器的使用方法. 一个小难题 我们继续完善“MVC公告发布系统”, ...
- 利用formdata对象上传文件时,需要添加的参数
function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'h ...
- 【2017中国大学生程序设计竞赛 - 网络选拔赛 hdu 6150】Vertex Cover
[链接]点击打开链接 [题意] 有人写了一个最小点覆盖的贪心算法,然后,让你去hack它. 并且,要求这个算法得到的错误答案,是正确答案的三倍. 让你任意输出hack数据,点数<=500 [题解 ...