[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服务 ...
随机推荐
- 让新版Chrome支持本地跨域请求调试
1.创建一个Chrome的启动快捷方式: 2.右键点击快捷方式属性,然后在目标路径后面,添加以下参数: --disable-web-security --user-data-dir="e:\ ...
- 活动a 使用 启动为结果 方法 启动 活动 b, b什么都不做 并返回给a,a中的 在活动结果时候 回调 是否被执行?
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 活动a 使用 启动为结果 方法 启动 活动 b, b什么都不做 并返回给a,a中的 在活 ...
- 【UOJ #107】【APIO 2013】ROBOTS
http://uoj.ac/problem/107 设\(f(l,r,i,j)\)表示\([l,r]\)中的机器人聚集到\((i,j)\)需要花的最小操作数. \(f(l,r,i,j)=\min\le ...
- 【20181026T1】**手枪【dfs】
题面 [错解] 百年难得一见之提高考搜索了 ...怎么搞啊 相当于是S进去有一个环? tarjan? 跑个联通块,可以穿过去的连一条边? 好主意-- dfs写完了-- 哎等下? 5 5 .##.. # ...
- 【lct】bzoj1036 [ZJOI2008]树的统计Count
题意:给你一棵树,点带权,支持三种操作:单点修改:询问链上和:询问链上max. 这里的Query操作用了与上一题不太一样的做法(上一题用那种做法,因为在边带权的情况下换根太困难啦): 先ChangeR ...
- 【Kruskal+贪心思想】BZOJ3624-[Apio2008]免费道路
国庆万岁!!!!! [题目大意] 给定一张无向图,有两种边的类型为0和1.求一个最小生成树使得边0有k条. [思路] 跑两次Kruskal. 第一次的时候优先选择边1,然后判断有哪些边0还不能连通,那 ...
- 【LCA/tarjan】POJ1470-Closest Common Ancestors
[题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...
- Eclipse下使用Stanford CoreNLP的方法
源码下载地址:CoreNLP官网. 目前release的CoreNLP version 3.5.0版本仅支持java-1.8及以上版本,因此有时需要为Eclipse添加jdk-1.8配置,配置方法如下 ...
- 1.8(SQL学习笔记)触发器
一.触发器简介 当需要某些操作在某些语句执行之前或之后执行就需要使用触发器. 例如每次插入数据时进行数据校对,每次删除数据后将删除内容备份到新表. 这些操作我们希望它(某些语句)在满足某些条件时自动执 ...
- Tarjan 算法详解
一个神奇的算法,求最大连通分量用O(n)的时间复杂度,真实令人不可思议. 废话少说,先上题目 题目描述: 给出一个有向图G,求G连通分量的个数和最大连通分量. 输入: n,m,表示G有n个点,m条边 ...