[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 : 40
                      }, {
                          page : ‘About‘,
                          view : 10
                      }, {
                          page : ‘Help‘,
                          view : 4
                      });

const Sum = x => ({
    x,
    concat   : ({ x: y }) => Sum(x + y),
    toString : () => `Sum(${x})`
});
Sum.empty = () => Sum(0);

const res = stats.foldMap(x => fromNullable(x.view)
.map(Sum), Right(Sum(0)));
console.log(res.toString()); // Right(Sum(54))

If change the data a litte bit:

let stats = List.of({
                          page : ‘Home‘,
                          view : 40
                      }, {
                          page : ‘About‘,
                          view : 10
                      }, {
                          page : ‘Help‘,
                          view : null
                      });

const Sum = x => ({
    x,
    concat   : ({ x: y }) => Sum(x + y),
    toString : () => `Sum(${x})`
});
Sum.empty = () => Sum(0);

const res = stats.foldMap(x => fromNullable(x.view)
.map(Sum), Right(Sum(0)));
console.log(res.toString()); // Right(Sum(50))

Because the view: null, then it will skip .map(sum).

时间: 2024-11-03 22:27:15

[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 so it's not a safe operation, whereas with the monoids we could take as many as we possibly want, even none, and still return us back something. It's a

很好的iOS学习资料

https://github.com/vsouza/awesome-ios 汇集了很多好的资料 https://github.com/vsouza/awesome-ios Skip to content This repository Pull requests Issues Gist You don’t have any verified emails. We recommend verifying at least one email. Email verification helps ou

这些国外前端周刊值得你收藏一下

鄙人最近发现自己邮箱很多未读邮件,怕怕:查看大多数都是自己之前订阅前端相关的. 现在在这里也给大家分享分享...带好你们手头的邮箱了,要开车了. 一.nodeweekly [戳我带飞] A free, once–weekly e-mail round-up of Node.js news and articles. 一个免费的.一周一更新发布Node.js新闻和文章到邮箱里. 二.webopsweekly [戳我带飞] A weekly newsletter on Web operations,

集合框架学习之Guava Collection

开源工具包: Guava : Google Collection Apache:Commons Collecton 1.1 Google Collections Guava:google的工程师利用传说中的"20%时间"开发的集合库,它是对jdk提供的扩展,提供了很多使用的类来简化代码 jar包:https://code.google.com/p/guava-libraries/ 源码下载: 下载git工具:(易于本地增加分支和分布式的特性) msysgit:http://code.g

[Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types

A frequent use case when transducing is to apply a transformation to items without changing the type of the collection. In this lesson, we'll create a helper to do just that. seq will be similar to into, except the target type will be inferred from t

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

Docker Compose 一键部署Nginx代理Tomcat集群 目录结构 [[email protected] ~]# tree compose_nginx_tomcat/ compose_nginx_tomcat/ ├── docker-compose.yml ├── mysql │   ├── conf │   │   └── my.cnf │   └── data ├── nginx │   ├── Dockerfile │   ├── nginx-1.12.1.tar.gz │  

Windows 系统安装Docker Compose 步骤

参考 Docker Compose official 官方安装指南: https://docs.docker.com/compose/install/ 实际上到目前为止还不能直接在Windows上安装Docker Compose,所以这篇文章要讲的是如何在Windows上使用Docker Compose. 先决条件: Docker Machine 已经安装完毕. 解决方案: 将 Docker-Compose 安装在Boot2Docker虚拟机中. 1. SSH to VM: $ docker-m

SharePoint 2010/SharePoint 2013 Custom Action: 基于Site Collection 滚动文字的通知.

应用场景: 有时候我们的站点需要在每个页面实现滚动文字的通知,怎么在不修改Master Page的情况下实现这个功能?我们可以使用Javascript 和 Custom Action 来实现. 创建一个Custom Action.主要使用到 Location = 'ScriptLink' 属性, 该属性可以动态的加载JavaScript 文件链接和代码块到模板页.代码如下: <Elements xmlns="http://schemas.microsoft.com/sharepoint/&

从头认识java-15.2 Collection的常用方法

这一章节我们来介绍一下Collection的常用方法. 我们下面以ArrayList为例. package com.ray.ch14; import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String[] args) { ArrayList<Integer> rtnList = new ArrayList<Integer>(); rtnL