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的更多相关文章

  1. [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 ...

  2. DotNet 资源大全中文版(Awesome最新版)

    Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...

  3. Frontend Development

    原文链接: https://github.com/dypsilon/frontend-dev-bookmarks Frontend Development Looking for something ...

  4. 【资源大全】.NET资源大全中文版(Awesome最新版)

    算法与数据结构(Algorithms and Data structures) 应用程序接口(API) 应用程序框架(Application Frameworks) 模板引擎(Application ...

  5. nlp-roadmap

    nlp-roadmap https://github.com/graykode/nlp-roadmap nlp-roadmap is Natural Language Processing ROADM ...

  6. 集合框架学习之Guava Collection

    开源工具包: Guava : Google Collection Apache:Commons Collecton 1.1 Google Collections Guava:google的工程师利用传 ...

  7. Docker Compose 一键部署Nginx代理Tomcat集群

    Docker Compose 一键部署Nginx代理Tomcat集群 目录结构 [root@localhost ~]# tree compose_nginx_tomcat/ compose_nginx ...

  8. (转)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 ...

  9. [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 ...

随机推荐

  1. 69.fprintf fscanf

    fprintf //从读文件中提取字符串到info1.user和info1.password中 fscanf(pfr, "%s%s", info1.user, info1.pass ...

  2. js 限制只能输入数字小数点

    function checkNum(e) { var re = /^\d+(?=\.{0,1}\d+$|$)/ if (e.value != "") { if (!re.test( ...

  3. xml数据文件上传至数据库

    上传xml文件数据到数据库思路:首先上传需要建校验xml字段的校验文件,然后程序根据后台写好的xml数据文件路径自动读取数据文件,再上传数据文件到数据库之前先根据校验文件校验上传的数据文件的字段是否合 ...

  4. CODEVS——T1332 上白泽慧音 || 洛谷——P1726 上白泽慧音

    http://codevs.cn/problem/1332/|| https://www.luogu.org/problem/show?pid=1726#sub  时间限制: 1 s  空间限制: 1 ...

  5. 词向量 word2vec

    看的这一篇的笔记 http://licstar.net/archives/328 看不太懂. 要学的话,看这里吧,这里把一些资料做了整合: http://www.cnblogs.com/wuzhitj ...

  6. 104.tcp多线程读写实现群聊

    客户端: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <w ...

  7. js取对象的属性值循环

    var data = {name: "liuyang", job: "web", age: "27"} Object.keys(data). ...

  8. 一个简单http请求的jmeter压测实战流程

    1.新建线程组 2.创建http请求 注意:接口路径中的参数值要写变量 3.创建txt文件,存多个参数值 4.创建csv文件,在csv中上传txt文件 5.variable name填写txt中参数值 ...

  9. android问题及其解决-优化listView卡顿和怎样禁用ListView的fling

    问题解决-优化listView卡顿和怎样禁用ListView的fling 前戏非常长,转载请保留出处:http://blog.csdn.net/u012123160/article/details/4 ...

  10. 【转】CentOS/RHEL/OracleLinux使用UDEV配置ASMDISK

    转自:http://blog.csdn.net/staricqxyz/article/details/8332566 RHEL 5 / CentOS 5 / Oracle Linux 5 [root@ ...