[JS Compose] 6. Semigroup examples
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的更多相关文章
- [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 ...
- [JS Compose] 5. Create types with Semigroups
An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...
- [Ramda] Curry, Compose and Pipe examples
const curry = R.curry((fns, ary) => R.ap(fns, ary)); ), R.add()]); ,,]); console.log(res); //[2, ...
- [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 ...
- 用three.js创建一个简易的天空盒
本文创建的天空盒是用六张图片来创建的.笔者会论述两种方法来创建,都是最简单基本的方法,不涉及着色器的使用.一种是创建一个盒子,然后将图片作为盒子6个面的纹理贴上来创建.另一种则是简单的将纹理作为场景的 ...
- 【翻译】Ext JS 6.2 早期访问版本发布
原文:Announcing Ext JS 6.2 Early Access 非常开心,Sencha Ext JS 6.2早期访问版本今天发布了.早期访问版本的主要目的是为了让大家进行测试并评估Ext ...
随机推荐
- php课程 12-41 多态是什么
php课程 12-41 多态是什么 一.总结 一句话总结:一种请求,多种响应(根据参数类型和数量) 1.function useUsb(Usb $usb){}这句话是什么意思? 判断$usb参数是否实 ...
- FreeNX
freenx 在Linux下,我们最常使用的远程管理工具是ssh客户端,比如putty.SecureCRT等,但是ssh只提供字符界面的操作方式,有时我们需要图形界面的操作,在Linux下支持图形界面 ...
- 4.Windows下安装ZooKeeper
转自:https://www.cnblogs.com/mstmdev/p/5612791.html 官方主页: https://zookeeper.apache.org/ 选择合适的镜像地址下 ...
- jQuery快速入门知识重点
1.jquery中attr与prop的区别 attr:是通过setAttribute 和 getAttribute来设置的使用的是DOM属性节点 prop:是通过document.getEle ...
- Android 软键盘弹出,界面整体上移的问题
AndroidManifest.xml文件中界面对应的<activity>里加入android:windowSoftInputMode="adjustPan" 键盘就会 ...
- win8.1 “服务器运行失败”的解决方法
平台:win8.1 SP1 问题:安装QQ安全管家又卸载后出现了奇怪的问题,1.在桌面点右键→个性化时,提示“服务器运行失败”.2.右键点击“这台电脑”,选择“属性”时没有反应.3.开始屏幕里随便选择 ...
- 芯片SA58672(功放芯片)
1::下面的中文图可能不准: 针对上面的图,数据手册中有一些参数的推导: 这个式子是电压增益的 这个式子是关于截止频率的 典型原理图: 需要电源去耦,能够提高效率. PVDD引脚处的电 ...
- pcb过孔盖油
pcb的过孔应该盖油,,这样,两个距离比较紧的过孔就不会在焊接的时候短路了,尤其是手工焊接小件的时候.
- P2P网贷中的4种理财业务模式
线上3种 直投标:线上理财人直接购买借款人的标,平台只是起个"撮合"作用,收点借款人的服务费. 借款人不还钱,有的平台会帮"借款人"还 ...
- 【BZOJ 4310】跳蚤
[链接]h在这里写链接 [题意] 给你一个字符串; 让你把它分割成最多k个部分. 然后求出每个部分的字符串里面子串的字典序最大的那一个子串. 然后在这k ...