[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服务 ...
随机推荐
- UVA12995 Farey Sequence [欧拉函数,欧拉筛]
洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...
- 关于 bitset 的一些题目
参考 http://www.cplusplus.com/reference/bitset/bitset/ https://blog.csdn.net/snowy_smile/article/detai ...
- Xamarin.Forms XAML的辅助功能Code Snippet
Xamarin.Forms XAML的辅助功能Code Snippet 在Visual Studio中,使用Code Snippet(代码片段)功能可以减少基础代码的编写量,如常见的标签.循环语句 ...
- FastReport.Net使用:[18]形状(Shape)控件用法
FastReport中,如果要画一张漂亮的报表,经常会画些形状控件来美化?那么如何用好形状(Shape)控件呢? 形状的5种类型 在工具栏的图形控件下拉菜单中有5种类型(矩形.圆角矩形.椭圆形.三角形 ...
- Entity Framework(实体框架 EF)
什么是Entity Framework呢(下面简称EF)? EF(实体框架)是ADO.NET中的一组支持开发面向数据的软件应用程序的技术,是微软的一个ORM框架.ORM(对象关系映射框架):指的是面向 ...
- 性能测试工具——Jmeter使用小结(一)
Apache Jmeter是针对Java的一款性能测试工具,利用该工具可以实现自动化的批量测试和结果聚合,适合做接口压测.今天就来捋一捋软件安装的一些小细节和使用. 一.安装 Jmeter基于JDK, ...
- python开发_count()
python中的count()函数,从字面上可以知道,他具有统计功能 下面来看看具体的demo: 功能:读取一个文件'c:\\test.txt',统计出该文件中出现字符'a'的次数 #python o ...
- redis配置参数简介
redis配置查看方式: 1.redis的安装目录查看redis.conf 2.登陆redis客户端,使用 config get xx命令. 比如:查看所有的配置: config get * [roo ...
- How to open a web site with the default web browser in a NEW window
http://delphi.about.com/cs/adptips2004/a/bltip0504_4.htm When using ShellExecute (as explained in th ...
- TCP通信粘包问题分析和解决(全)(转)
TCP通信粘包问题分析和解决(全) 在socket网络程序中,TCP和UDP分别是面向连接和非面向连接的.因此TCP的socket编程,收发两端(客户端和服务器端)都要有成对的socket,因此,发送 ...