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. JS 实现图片模态框,幻灯片,跑马灯功能

    网站中常用的幻灯片和模态框,使用 HTML.JavaScript 与 CSS 来创建 Lightbox,类似相册预览功能.可以运用到视频网站,商城,相册上去 参考了菜鸟教程,有兴趣自己去看 HTML/ ...

  2. Linux登录状态

    1.hostname 显示主机名称.设置主机名称 1)显示主机名称 xiaohuang@xiaohuang-virtual-machine:~$ hostname xiaohuang-virtual- ...

  3. 事件循环(Event Loop)

    1.什么是事件循环? JavaScript为单线程执行的,所以是从上到下依次执行,js分为两个任务,宏任务和微任务 首先执行宏任务(第一次就是执行所有的同步代码),再执行所有的微任务,执行完毕之后再次 ...

  4. mysql集群搭建教程-基础篇

           计算机一级考试系统要用集群,目标是把集群搭建起来,保证一个库dang了,不会影响程序的运行.于是在孟海滨师哥的带领下开始了我的第一次搭建mysql集群,首先看了一些关于集群的资料,然后根 ...

  5. menu-普通menu弹出框样式

    今天接触到了menu弹出框样式.主要就是在theme下进行调整.现在把接触到的知识点总结一下. 在theme中,跟menu有关的几个属性如下 <item name="panelBack ...

  6. Python *的下载、安装和使用

    最近正在学习机器学习和深度学习,需要python,大家都知道,它是这些领域的入门语言,不多说,直接上干货!突然发现python比java好玩多了 下面就开始python的安装以及测试,python2. ...

  7. JS学习笔记 - fgm练习 - 数字自增 定时器 数字比大小Math.max

    <script> window.onload = function(){ var oP = document.getElementsByTagName('p')[0]; var i = 0 ...

  8. uva 10098 Generating Fast(全排列)

    还是用的两种方法,递归和STL,递归那个是含有反复元素的全排列,这道题我 没有尝试没有反复元素的排列,由于从题目上并没有发现一定是有反复元素的() 贴代码: <span style=" ...

  9. Altium Designer如何删除以布的线

  10. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...