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. 最小生成树(MST,minimum spanning tree)

    生成树:由图生成的树,由图转化为树,进一步可用对树的相关操作来对图进行操作.最小指的是权值最小: 生成树是边的集合,如下图所示的最小生成树:MST={{a,b},{a,f},{f,c}} 本文主要探讨 ...

  2. vue ---webpack 打包上线

     先来描述一下期间遇到的问题有哪些: 1.打包后将 dist 文件夹和 index.html 放到 tomcat,在浏览器中访问时,出现空白页,f12 提示 404. 2.打包好的静态资源均是绝对路径 ...

  3. C语言的多行宏定义

    一.多行宏定义的使用 最近在跟STM32L011K4T6低功耗的源代码,发现使用了多行的宏定义来封装函数,记得之前把\给删除掉,编译程序就一直报错. \是续行操作符,也就是宏定义一行写不完,需要多行写 ...

  4. C#创建子线程,子线程使用委托更新控件

    一.背景 由于在窗体程序中通过点击一个button按键后需要更新TreeView控件的内容,由于等待时间比较长,主程序无法一起在那边等待,需要去处理其它的事情,所以就需要创建新的子线程来处理.因为主线 ...

  5. Android滚轮选择器实现

    思路: 1.布局,整个控件的布局,事实上就是用代码取带xml来实现当前布局 2,能够滑动的(即滚轮).事实上是一个ScrollView 3.推断滑动状态的,有protected void onScro ...

  6. 网页设计实战3 ufo类型的科技网页如何实现

    网页设计实战3 ufo类型的科技网页如何实现 一.总结 一句话总结:基础的图片素材就是如何几张图片,这个效果只是通过jquery或者js让那个png图片旋转起来了,如此而已.其实核心就是一个trans ...

  7. 微信支付v2开发(9) 标记客户投诉处理状态

    本文介绍微信支付中如何标记客户投诉的处理状态. 一.API Api 的 url 为: https://api.weixin.qq.com/payfeedback/update?access_token ...

  8. AndroidStudio MAT LeakCanary 内存分析之 LeakCanary

    现在我们换一种更清晰方便的方式:LeakCanary https://github.com/square/leakcanary 首先将LeakCanary绑在我们的app上 build.gradle ...

  9. js进阶 13-7 如何实现滑动面板效果

    js进阶 13-7 如何实现滑动面板效果 一.总结 一句话总结:就是普通的jquery动画中的滑动效果.$('#content').slideToggle().滑动效果的实质是通过调整高度. 1.滑动 ...

  10. (转)windows 下 Java 及 Python 环境变量设置

    转自:http://www.cnblogs.com/zhj5chengfeng/archive/2013/01/01/2841253.html http://www.cnblogs.com/qiyes ...