[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 ...
随机推荐
- Atcoder AtCoder Regular Contest 079 E - Decrease (Judge ver.)
E - Decrease (Judge ver.) Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem Statem ...
- 创建VG
创建VG smit mkvg Add a Volume Group Add a Scalable Volume Group VOLUME GROUP name ...
- JS 实现图片模态框,幻灯片,跑马灯功能
网站中常用的幻灯片和模态框,使用 HTML.JavaScript 与 CSS 来创建 Lightbox,类似相册预览功能.可以运用到视频网站,商城,相册上去 参考了菜鸟教程,有兴趣自己去看 HTML/ ...
- wps如何输入连续的长破折号
最近在写论文, 想输入破折号,结果是— — 这个破折号中间是有缝隙的, 如何变成没有缝隙. 第一步,选中: 第二步,右击选择字体 第三步,放大(只加一个破折号,然后放大到200%) 不知道有没有人,像 ...
- 分析器错误消息: 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分
关于错误提示:此实现不是 Windows 平台 FIPS 验证的加密算法的一部分的解决方案 不知怎么的,每次Win10升级后相应的注册器都恢复默认了,当我运行08版的asp项目时会报这个错. vs上的 ...
- datetime小练习
题目: 1.计算你的生日比如近30年来(1990-2019),每年的生日是星期几,统计一下星期几出现的次数比较多2,生日提醒,距离生日还有几天 # !/usr/bin/env python # -*- ...
- 今天遇到奇怪的事:SVN本地代码的标记突然没了,Clean up也报错
今天遇到奇怪的事:SVN本地代码的标记突然没了.Clean up也报错 脑子一想这样的情况,能够先把原来的文件夹改一个名字.又一次把代码check out下来,再合并提交更新,但这样也太LOW了吧 上 ...
- JQuery EasyUI Combobox 实现省市二级联动菜单
//编辑改动或新增页面联动能够这样写 jQuery(function(){ // 省级 $('#province').combobox({ valueField:'itemvalue', //值字段 ...
- HttpWatch--time chart分析
这是一个IE的插件,下载可以点这里.下载后解压如下图所示,一共有4个文件.HttpWatch Professional是单独软件,可以单独使用. 解压后有四个文件 插件安装时,只需运行httpwatc ...
- java调用C++的过程
转自https://blog.csdn.net/yjhdxflqm/article/details/50503551 jni是java和C.C++通信的桥梁. java适合写上层的应用,C.C++适合 ...