[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 perfectly safe operation here that we can reduce as many of them as we'd like.
For example to Sum():
const Sum = x =>
({
concat: o => Sum(x + o.x)
})
'Zero' neutral element for Sum semi-group, so Sum is monoids.
+ //
+ //
x + //x
So we can define an interface for Sum:
Sum.empty = () => Sum()
And if we concat Sum.empty to anything, it won't affect the result:
Sum.empty().concat(Sum()) // Sum(1)
The same as All():
All.empty = () => All(true) // true && true --> true
// false && true --> false
But for the First(), we can not find a neutal element for it, because it just throw away the rest value only keep the first value and first value can be undefined.
[,,] && undefined -->
undefined && [,,] --> error
Monodis also looks like reduce:
const sum = xs =>
xs.reduce((acc, x) => acc + x, ) console.log(sum([,,])) // const all = xs =>
xs.reduce((acc, x) => acc && x, true) console.log(all([true, false])) //false const first = xs =>
xs.reduce((acc, x) => acc) console.log(first([,,]))
console.log(first([])) //Error
[JS Compose] 7. Ensure failsafe combination using monoids的更多相关文章
- [JS Compose] 0. Understand 'Box' or 'Container', they are just like Array!
We'll examine how to unnest function calls, capture assignment, and create a linear data flow with a ...
- [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
We refactor a function that uses try/catch to a single composed expression using Either. We then int ...
- [JS Compose] 2. Enforce a null check with composable code branching using Either
We define the Either type and see how it works. Then try it out to enforce a null check and branch o ...
- [JS Compose] 1. Refactor imperative code to a single composed expression using Box
After understanding how Box is, then we are going to see how to use Box to refacotr code, to un-nest ...
- [Compose] 8. A curated collection of Monoids and their uses
const { List } = require('immutable-ext'); const Right = x => ({ chain : f => f(x), ap : other ...
- [JS Compose] 6. Semigroup examples
Let's we want to combine two account accidently have the same name. , friends: ['Franklin'] } , frie ...
- [JS Compose] 5. Create types with Semigroups
An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...
- MongoDB学习(2)—Node.js与MongoDB的基本连接示例
前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务,在test数据库中插入一条实例数据: db. ...
- Node.js与MongoDB的基本连接示例
Node.js与MongoDB的基本连接示例 前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务 ...
随机推荐
- Python并发编程-多进程socketserver简易版
普通版的socketserver #server.py import socket sk = socket.socket() sk.bind(('127.0.0.1',8080))#建立连接 sk.l ...
- 【JAVAWEB学习笔记】22_ajax:异步校验用户名和站内查询
Js原生Ajax和Jquery的Ajax 学习目标 案例1-异步校验用户名是否存在 案例2-站内查询 一.Ajax概述 1.什么是同步,什么是异步 同步现象:客户端发送请求到服务器端,当服务器返回响应 ...
- 2016 湖南省省赛B题《有向无环图》
题目链接[https://vjudge.net/problem/CSU-1804] 题意: 给出一个有向无环图,然后让你算下面的结果,count(i,j)表示i->j之间的路径条数. 题解: 根 ...
- 主席树+dfs SPOJ BZOJ2588 Count on a tree
这道题我由于智障错误导致一直错. 在树上建主席树,加上lca思想,很简单. #include<bits/stdc++.h> using namespace std; ; struct no ...
- [BZOJ 3720][JZYZOJ 2016]gty的妹子树 强制在线 树分块/树套树
jzyzoj的p2016 先码着,强制在线的树分块或者树套树?关键是我树分块还在入门阶段树套树完全不会啊摔 http://blog.csdn.net/jiangyuze831/article/de ...
- poj 1485 dp
转自:http://www.cnblogs.com/kuangbin/archive/2011/11/12/2246407.html [题目大意] 一条公路上有n个旅馆,选出其中k个设置仓库,一个仓库 ...
- Makefile-有三个非常有用的变量。分别是$@,$^,$<代表的意义
$@ 代表目标文件,$^ 代表所有的依赖文件,$< 代表第一个依赖文件. # 这是简化后的Makefilemain:main.o mytool1.o mytool2.o gcc -o $@ $^ ...
- bzoj1798 维护序列
Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2 ...
- ACM -- 算法小结(十)素数的两种打表法
素数的两种打表法 下面介绍两种素数打表法,由于是两年前留下的笔记,所以没有原创链接~~ @_@!! 第一种疯狂打表法: #include<stdio.h> #include<math ...
- [转]android中drawable资源的解释及例子
原文链接: http://blog.csdn.net/wode_dream/article/details/38584693 文章中的内容参考Dev Guide中的Drawable R ...