[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 ...
随机推荐
- BZOJ 3456 城市规划 ( NTT + 多项式求逆 )
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意: 求出\(n\)个点的简单(无重边无自环)无向连通图的个数.(\(n< ...
- 1.25 Python知识进阶 - 封装
封装 示例代码: class Role(object): count = 0 def __init__(self,name,role,weapon,life_value=100,money=15000 ...
- Fedora 13 Alpha测试手记横空出世
本文转载在:http://www.linux521.com/2009/system/201004/10719.html 本文是继<Fedora 11-Alpha试用手记>文章(http ...
- golang md5
package main import ( "crypto/md5" "encoding/hex" "fmt" "io" ...
- JavaScript推断undefined的技巧
两种方法: 处理变量为undefined的情况: v = v||null; //假设v为undefined,则其值变为null 双感叹号:!!,把null/undifined/0转换为bolle ...
- 几种基于Java的SQL解析工具的比较与调用
1.sqlparser http://www.sqlparser.com/ 优点:支持的数据库最多,除了传统数据库外还支持hive和greenplum一类比较新的数据库,调用比较方便,功能不错 缺点: ...
- 【例题 7-10 UVA - 11212】Editing a Book
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜. 很容易想到,最多只要搜8层就可以得到答案了 ->最多8下肯定可以还原. 则枚举一下最大层数.然后做个深搜就好. ...
- 洛谷 P1601 A+B Problem(高精)
P1601 A+B Problem(高精) 题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式 ...
- 常用的织梦dedecms安全设置集合整理
织梦系统用户很多,被发现的漏洞也就相对很多,所以网站安全需要做好,很多所谓的“黑客”都是用工具来扫描入侵,厉害点的人是不屑来黑我们的小网站的,所以在我们不是专业维护人员情况下,做好一般的安全防护就可以 ...
- WinXP局域网共享设置
关闭局域网共享 1.不允许SAM帐户和共享的匿名枚举(系统默认是允许的). 组策略-计算机配置-Windows 设置-安全设置-本地安全策略-安全选项-网络访问:不允许SAM帐户和共享的匿名枚举. 设 ...