Let's we want to combine two account accidently have the same name.

const acct1 = { name: 'Nico', isPaid: true, points: , friends: ['Franklin'] }

const acct2 = { name: 'Nico', isPaid: false, points: , friends: ['Gatsby'] }

So, here we can use Semi-group to combine them, because the semi-group have the knowledge how to combine for each type of object.

So we change two accounts as:

const acct1 = { name: First('Nico'), isPaid: All(true), points: Sum(), friends: ['Franklin'] }

const acct2 = { name: First('Nico'), isPaid: All(false), points: Sum(), friends: ['Gatsby'] }

But here we still have one problem which Object doesn't have 'concat' method, so we need to use Immutable library to help:

Includes libarary:

const {Map} = Immutable;
const acct1 = Map({ name: First('Nico'), isPaid: All(true), points: Sum(), friends: ['Franklin'] })

const acct2 = Map({ name: First('Nico'), isPaid: All(false), points: Sum(), friends: ['Gatsby'] })

---------

const {Map} = Immutable;

const Sum = x =>
({
x,
concat: ({x: y}) =>
Sum(x + y),
inspect: () =>
`Sum(${x})`
}) const All = x =>
({
x,
concat: ({x: y}) =>
All(x && y),
inspect: () =>
`All(${x})`
}) const First = x =>
({
x,
concat: _ =>
First(x),
inspect: () =>
`First(${x})`
}) const acct1 = Map({ name: First('Nico'), isPaid: All(true), points: Sum(), friends: ['Franklin'] }) const acct2 = Map({ name: First('Nico'), isPaid: All(false), points: Sum(), friends: ['Gatsby'] }) const res = acct1.concat(acct2) // Showing results
console.log("Friend 1: ", res.toJS().friends[]) //Friend 1: Franklin
console.log("Friend 2: ", res.toJS().friends[]) //Friend 2: Gatsby
console.log("isPaid: ", res.toJS().isPaid.x) //isPaid: false
console.log("Name: ", res.toJS().name.x) // Name: Nico
console.log("Points: ", res.toJS().points.x) // Points: 12

[JS Compose] 6. Semigroup examples的更多相关文章

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

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

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

  3. [Ramda] Curry, Compose and Pipe examples

    const curry = R.curry((fns, ary) => R.ap(fns, ary)); ), R.add()]); ,,]); console.log(res); //[2, ...

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

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

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

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

  8. 用three.js创建一个简易的天空盒

    本文创建的天空盒是用六张图片来创建的.笔者会论述两种方法来创建,都是最简单基本的方法,不涉及着色器的使用.一种是创建一个盒子,然后将图片作为盒子6个面的纹理贴上来创建.另一种则是简单的将纹理作为场景的 ...

  9. 【翻译】Ext JS 6.2 早期访问版本发布

    原文:Announcing Ext JS 6.2 Early Access 非常开心,Sencha Ext JS 6.2早期访问版本今天发布了.早期访问版本的主要目的是为了让大家进行测试并评估Ext ...

随机推荐

  1. holder.js如何使用

    holder.js的使用 一.总结 一句话总结:使用:holder.js后面接图片宽高 <img src="holder.js/300x200" /> 1.holder ...

  2. ByteUtils

    package sort.bing.com; import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import j ...

  3. PMP杂谈--名词解释

    过程:PMP中常常提到过程,好像非常高大上,实则不然,过程说白了就是一系列的行动和活动,用来创建预定的产品,服务或成果. 就这么简单. 再深入点就是,行动和活动当然都会有ITTO(输入,工具和技术.输 ...

  4. ListView-divider 分割线的设置

    1.去掉分割线 android:divider="@null" 2.设置分割线颜色跟宽度 android:divider="#19000000" android ...

  5. java poi 向excel写入图片

    import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; impo ...

  6. 洛谷P1876 开灯

    题目背景 该题的题目是不是感到很眼熟呢? 事实上,如果你懂的方法,该题的代码简直不能再短. 但是如果你不懂得呢?那...(自己去想) 题目描述 首先所有的灯都是关的(注意是关!),编号为1的人走过来, ...

  7. [RxJS] How To get the results of two HTTP requests made in sequence

    switchMap can chain two HTTP requests together, creating one request based on the results of the fir ...

  8. 关于C++中用两个迭代器方式初始化string的知识

    string(iter1, iter2); 第一点:两个迭代器必须指向同一个容器. 第二点:iter2必须>=iter1. 第三点:假设iter1等于iter2,那么结果为空[] 另外一个比較特 ...

  9. Visual C# 2008 调试技巧

      1,非中断模式下的调试. 利用系统“输出”窗口.(视图-输出)来打印调试信息.有Debug和Release两种版本,通过运行按钮右边的选项可以选择程序的运行方式.而对应的现实调试信息的方法也不同. ...

  10. postman--基本使用1

    本文转自:http://blog.csdn.net/five3/article/details/53021084 HTTP的接口测试工具有很多,可以进行http请求的方式也有很多,但是可以直接拿来就用 ...