1. const { List } = require('immutable-ext');
  2.  
  3. const Right = x => ({
  4. chain : f => f(x),
  5. ap : other => other.map(x),
  6. traverse : (of, f) => f(x).map(Right),
  7. map : f => Right(f(x)),
  8. fold : (f, g) => g(x),
  9. concat : o => o.fold(_ => Right(x), y => Right(x.concat(y))),
  10. toString : () => `Right(${x})`
  11. })
  12.  
  13. const Left = x => ({
  14. chain : f => Left(x),
  15. ap : other => Left(x),
  16. traverse : (of, f) => of(Left(x)),
  17. map : f => Left(x),
  18. fold : (f, g) => f(x),
  19. concat : o => o.fold(_ => Left(x), y => o),
  20. toString : () => `Left(${x})`
  21. });
  22.  
  23. const fromNullable = x => x != null ?
  24. Right(x) :
  25. Left(null);
  26.  
  27. const tryCatch = f => {
  28. try {
  29. return Right(f())
  30. }
  31. catch( e ) {
  32. return Left(e)
  33. }
  34. };
  35.  
  36. let stats = List.of({
  37. page : 'Home',
  38. view :
  39. }, {
  40. page : 'About',
  41. view :
  42. }, {
  43. page : 'Help',
  44. view :
  45. });
  46.  
  47. const Sum = x => ({
  48. x,
  49. concat : ({ x: y }) => Sum(x + y),
  50. toString : () => `Sum(${x})`
  51. });
  52. Sum.empty = () => Sum();
  53.  
  54. const res = stats.foldMap(x => fromNullable(x.view)
  55. .map(Sum), Right(Sum()));
  56. console.log(res.toString()); // Right(Sum(54))

If change the data a litte bit:

  1. let stats = List.of({
  2. page : 'Home',
  3. view :
  4. }, {
  5. page : 'About',
  6. view :
  7. }, {
  8. page : 'Help',
  9. view : null
  10. });
  11.  
  12. const Sum = x => ({
  13. x,
  14. concat : ({ x: y }) => Sum(x + y),
  15. toString : () => `Sum(${x})`
  16. });
  17. Sum.empty = () => Sum();
  18.  
  19. const res = stats.foldMap(x => fromNullable(x.view)
  20. .map(Sum), Right(Sum()));
  21. 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. thinkphp验证码(总结之后,效率非常好)

    thinkphp验证码(总结之后,效率非常好) 一.总结 1.总结:总结之后,效率非常好,复习的时候也才方便,也能深入到细处,非常方便建立熟悉感和突破使用困境 2.验证码使用中文集或者繁体集设置:这个 ...

  2. canvas:动态时钟

    此时针是以画布的中心为圆心: ctx.translate(width/2,width/2); 此函数是将画布的原点移到(width/2,width/2) 数字的位置我们利用了三角函数的原理 x=rco ...

  3. 7.Linux 输入子系统分析

    为什么要引入输入子系统? 在前面我们写了一些简单的字符设备的驱动程序,我们是怎么样打开一个设备并操作的呢? 一般都是在执行应用程序时,open一个特定的设备文件,如:/dev/buttons .... ...

  4. CISP/CISA 每日一题 16

    CISA 每日一题(答) 作业调度软件的优点: 1.作业信息仅需建立一次,减少错误发生概率: 2.可定义作业间的依赖关系,当某一项作业失败时,依赖于该作业的后续作业就不会被执行: 3.所有成功或失败的 ...

  5. Spring学习总结(9)——Spring AOP总结

    spring IOC和AOP是Spring框架的两大核心基石,本文将对Spring AOP做一个系统的总结. 什么是AOP AOP(Aspect-Oriented Programming,面向切面编程 ...

  6. Shiro学习总结(3)——Apache Shiro身份认证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  7. VPS的centOS6安装远程桌面

    VPS的centOS6安装远程桌面 64位系统的需要编译安装 ttp://www.landui.com/help/Show-991.html xrdp是在图形界面下使用的,首先要确定您的centos系 ...

  8. Android开发之搜芽项目的图片载入问题(使用Volley进行网络图片载入)

    搜芽的移动开发这几天进度相对来说很的快. 可是美中不足的就是网络图片的载入问题. 我有两套方案: 1)沿用迅雷动漫的图片载入.迅雷动漫也是用的一个开源的库.可是不知道是我使用出了问题还是真的是它的问题 ...

  9. 接口如何使用(以笑话大全api为例)

    接口如何使用(以笑话大全api为例) 一.总结 一句话总结:直接用ajax,或者post,get方式向接口网址请求数据,然后接收网站传过来的数据就好,和我们写网站的时候前台向后台请求数据的方式一样. ...

  10. vue给对象新添加属性,一定要使用Vue.set( target, key, value )这个API来添加

    this.tagList = [{ id:1, tagName:'90后' }, { id:2, tagName:'土豪' }, { id:3, tagName:'美女' }, { id:4, tag ...