[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 ...
随机推荐
- thinkphp验证码(总结之后,效率非常好)
thinkphp验证码(总结之后,效率非常好) 一.总结 1.总结:总结之后,效率非常好,复习的时候也才方便,也能深入到细处,非常方便建立熟悉感和突破使用困境 2.验证码使用中文集或者繁体集设置:这个 ...
- canvas:动态时钟
此时针是以画布的中心为圆心: ctx.translate(width/2,width/2); 此函数是将画布的原点移到(width/2,width/2) 数字的位置我们利用了三角函数的原理 x=rco ...
- 7.Linux 输入子系统分析
为什么要引入输入子系统? 在前面我们写了一些简单的字符设备的驱动程序,我们是怎么样打开一个设备并操作的呢? 一般都是在执行应用程序时,open一个特定的设备文件,如:/dev/buttons .... ...
- CISP/CISA 每日一题 16
CISA 每日一题(答) 作业调度软件的优点: 1.作业信息仅需建立一次,减少错误发生概率: 2.可定义作业间的依赖关系,当某一项作业失败时,依赖于该作业的后续作业就不会被执行: 3.所有成功或失败的 ...
- Spring学习总结(9)——Spring AOP总结
spring IOC和AOP是Spring框架的两大核心基石,本文将对Spring AOP做一个系统的总结. 什么是AOP AOP(Aspect-Oriented Programming,面向切面编程 ...
- Shiro学习总结(3)——Apache Shiro身份认证
身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...
- VPS的centOS6安装远程桌面
VPS的centOS6安装远程桌面 64位系统的需要编译安装 ttp://www.landui.com/help/Show-991.html xrdp是在图形界面下使用的,首先要确定您的centos系 ...
- Android开发之搜芽项目的图片载入问题(使用Volley进行网络图片载入)
搜芽的移动开发这几天进度相对来说很的快. 可是美中不足的就是网络图片的载入问题. 我有两套方案: 1)沿用迅雷动漫的图片载入.迅雷动漫也是用的一个开源的库.可是不知道是我使用出了问题还是真的是它的问题 ...
- 接口如何使用(以笑话大全api为例)
接口如何使用(以笑话大全api为例) 一.总结 一句话总结:直接用ajax,或者post,get方式向接口网址请求数据,然后接收网站传过来的数据就好,和我们写网站的时候前台向后台请求数据的方式一样. ...
- vue给对象新添加属性,一定要使用Vue.set( target, key, value )这个API来添加
this.tagList = [{ id:1, tagName:'90后' }, { id:2, tagName:'土豪' }, { id:3, tagName:'美女' }, { id:4, tag ...