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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [Compose] 8. A curated collection of Monoids and their uses

    const { List } = require('immutable-ext'); const Right = x => ({ chain : f => f(x), ap : other ...

  6. [JS Compose] 6. Semigroup examples

    Let's we want to combine two account accidently have the same name. , friends: ['Franklin'] } , frie ...

  7. [JS Compose] 5. Create types with Semigroups

    An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...

  8. MongoDB学习(2)—Node.js与MongoDB的基本连接示例

    前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务,在test数据库中插入一条实例数据: db. ...

  9. Node.js与MongoDB的基本连接示例

    Node.js与MongoDB的基本连接示例 前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务 ...

随机推荐

  1. 洛谷P1503 鬼子进村 [平衡树,STL]

    题目传送门 鬼子进村 题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. ...

  2. shell 切分文件名提取文件扩展名或提取文件名

    有些脚本要根据文件名进行各种处理,有时候需要保留文件名抛弃文件后缀,也有时候需要文件后缀不要文件名,这类提取文件部分的操作使用shell的内建功能就能实现.需要用到的几个操作符有:%.%%.#.##. ...

  3. Redis学习篇(九)之生存时间

    EXPIRE 设置生存时间,以秒为单位 #### EXPIREAT 设置生存时间,秒时间戳格式 #### PEXPIRE 设置生存时间,毫秒为单位 #### PEXPOREAT 设置生存时间,毫秒时间 ...

  4. FastReport.Net使用:[18]形状(Shape)控件用法

    FastReport中,如果要画一张漂亮的报表,经常会画些形状控件来美化?那么如何用好形状(Shape)控件呢? 形状的5种类型 在工具栏的图形控件下拉菜单中有5种类型(矩形.圆角矩形.椭圆形.三角形 ...

  5. UOJ #30. [CF Round #278] Tourists

    UOJ #30. [CF Round #278] Tourists 题目大意 : 有一张 \(n\) 个点, \(m\) 条边的无向图,每一个点有一个点权 \(a_i\) ,你需要支持两种操作,第一种 ...

  6. [转]php-fpm - 启动参数及重要配置详解

    约定几个目录/usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.ini 一,php-fpm ...

  7. Android演示Stack(课下作业)

    Demand 之前活动中误传成别的截图,故在此补充博客 1.使用自己实现的栈构建Android程序,提供用于栈的一个puh按钮和pop按钮,在文本域接收一个字符串作为push的输入,文本区将显示每个操 ...

  8. 多个Fragment在屏幕翻转会重影问题的解决

    fragment使用add和hide而不用replace的方法添加到activity中,如果屏幕翻转可能会又add新的fragment进去,所以会重影. 如果有一个sparsearray保存fragm ...

  9. [COGS2426][HZOI 2016]几何

    [COGS2426][HZOI 2016]几何 题目大意: 给定平面坐标系内\(n\)个整点,求这些整点能构成的正多边形的边数的最大值. 思路: 一个基本结论:平面直角坐标系内能够形成的正多边形一定是 ...

  10. python开发_bisect

    现在有如下的需求: ''' 实现这样的一个功能: 对一个班级的学生的成绩做出一些评定,评定规则是: one: [0-60) -- F two: [60-70) -- D three: [70-80) ...