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. BZOJ3514: Codechef MARCH14 GERALD07加强版(LCT,主席树)

    Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密.接下来M ...

  2. 【C语言】编写函数实现库函数atoi,把字符串转换成整形

    //编写函数实现库函数atoi.把字符串转换成整形 #include <stdio.h> #include <string.h> int my_atoi(const char ...

  3. viewPager-基本实现示例

    直接看代码 package com.example.myviewpager; import android.app.Activity; import android.os.Bundle; import ...

  4. Spring MVC handler interceptors example--转载

    原文地址:http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/ Spring MVC allow you ...

  5. python 序列排序 排序后返回相应的索引

    https://blog.csdn.net/longwei92/article/details/83098289 https://blog.csdn.net/u013731339/article/de ...

  6. zeromq and jzmq

    install c test install jzmq java test Storm UI Cluster Summary Version Nimbus uptime Supervisors Use ...

  7. android问题及其解决-优化listView卡顿和怎样禁用ListView的fling

    问题解决-优化listView卡顿和怎样禁用ListView的fling 前戏非常长,转载请保留出处:http://blog.csdn.net/u012123160/article/details/4 ...

  8. Validation failed for query for method public abstract boxfish.bean.Student boxfish.service.StudentServiceBean.find(java.lang.String)!

    转自:https://blog.csdn.net/lzx925060109/article/details/40323741 1. Exception in thread "main&quo ...

  9. iOS_01_C语言简介

    1.先学C语言的原因 * oc基于C. * oc 跟 C的思想和语法很多地方不太一样,而且OC能和C混用. * C 是所有编程语言中的经典,很多高级语言都是从C语言中衍生出来的,比如 C++,C#.O ...

  10. spark源码解析之基本概念

    从两方面来阐述spark的组件,一个是宏观上,一个是微观上. 1. spark组件 要分析spark的源码,首先要了解spark是如何工作的.spark的组件: 了解其工作过程先要了解基本概念 官方罗 ...