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. 记录一下最近犯得sb的翻车错误

    首先是: 数据范围是long long范围,然后写了一个暴力,觉得过不去,于是开了int范围,最后写了个能骗过所有数据的骗分,然后没开longlong... 接着是: for(int i = l; i ...

  2. hihocoder #1580 : Matrix (DP)

    #1580 : Matrix 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Once upon a time, there was a little dog YK. On ...

  3. HDU 1057 What Are You Talking About trie树 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意 : 给一个单词表然后给一些单词,要求翻译单词表中有的单词,没有则直接输出原单词. 翻译文段部分get ...

  4. 并查集--CSUOJ 1601 War

    并查集的经典题目: CSUOJ 1601: War Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 247  Solved: 70[Submit][Sta ...

  5. 某DP题目2

    题意: 有一个栈,有n个数1~n按顺序插进栈中,但弹出顺序不定.另有m个限制,表示为a b,即数a必须在数b弹出之前弹出.问有多少种弹出的方案数.n <= 300,m <= 90000 分 ...

  6. c# -- 解决FromsAuthentication上下文不存在

    使用 FormsAuthentication.HashPasswordForStoringInConfigFile("需要加密的字符串", "MD5")这个方法 ...

  7. [转][译][Android基础]Android Fragment之间的通信

    2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用 ...

  8. 创建Server(tomcat)时候报Cannot create a server using the selected type

    1.退出 eclipse 2.到[工程目录下]/.metadata/.plugins/org.eclipse.core.runtime/.settings 3.把org.eclipse.wst.ser ...

  9. java - 内存泄漏

    内存泄漏问题产生原因 长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄露,尽管短生命周期对象已经不再需要,但是因为长生命周期对象持有它的引用而导致不能被回收,这就是java中内存泄露的发生场 ...

  10. JS 中对变量类型的判断

    总结:1. 一般简单的使用 typeof 或 instanceof 检测(这两种检测的不完全准确)          2. 完全准确的使用 原生js中的 Object.prototype.toStri ...