[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加密模块-pycryptodome
这个模块可以避开Pycrypto安装时带来的一系列包依赖问题. 安装命令: pip install pycryptodome 使用实例: from Crypto.Cipher import AES k ...
- 竹间智能科技(Emotibot)
竹间智能简仁贤:表情识别准确率达到81.57%,语义理解是主要的商用落地场景 北京-招聘机器学习(实习生) 深圳-招聘图像识别工程师
- Selenium之PhantomJS相关设置
设置PhantomJS请求头 默认情况下: from selenium import webdriver import time driver = webdriver.PhantomJS() driv ...
- PHP函数声明(二)
PHP的变量的范围 1.局部变量:在函数中声明的变量就是局部变量,只能在自己的函数内部使用. 2.全局变量:函数外声明,在变量声明以后的,直到整个脚本结束前都可以使用,包括在函数中和{}中都可以使用 ...
- Hibernate 使用MyEclipse简化开发
在平时开发中写配置文件比较繁琐,在这里写一下如何使用myEclipse简化开发. 1.打开MyEclipse,创建数据库连接 单机测试连接按钮,如果出现成功建立连接,则连接成功. 然后Finish 2 ...
- 用python slearning类库实现数据挖掘(python3.x)
Summary of test0 data : source data source code : in test0 file reference : - Reference Website / - ...
- PYPY_GC
Author:Jin Date: 2014-7-8 http://doc.pypy.org/en/latest/windows.html http://www.pypy.org/download.ht ...
- LT1961 升压型稳压器造就了兼具升压和降压能力的扁平状SEPIC
http://cds.linear.com/docs/cn/design-note/DN317%20sim_chs.pdf http://cds.linear.com/docs/en/datashee ...
- (转)SQL Server创建索引
什么是索引拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音.笔 ...
- glob函数的使用
glob库函数用于Linux文件系统中路径名称的模式匹配,即查找文件系统中指定模式的路径.注意,这不是正则表达式匹配,虽然有些相似,但还是有点差别. glob函数原型 #include & ...