An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a type with a concat method that are associative. We define three semigroup instances and see them in action.

 

A semigroup is a type with a concat method. Let's see if we have a string A, we can concat that with the string B. String is the semigroup here because it has a concat method. If we log this out here, we shall see the results AB and there we are.

"a".concat("b").concat("c"); //"abc"
"a".concat("b".concat("c")); //"abc"

We can also define our own semi-group:

const Sum = x =>
({
x, // we need to export x, so we can access it
concat: o => Sum(o.x + x), // o -> Sum(x)
toString: () => `Sum(${x})`
}); const res = Sum().concat(Sum());
console.log(res.toString()); // Sum(3)
const All = x => ({
x,
concat: o => All(o.x && x),
toString: ()=> `All(${x})`
}); const res = All(true).concat(All(false));
console.log(res.toString()); // All(false)
const First = x => ({
x,
concat: o => First(x),
toString: () => `First(${x})`
}); const res = First(true).concat(First(false));
console.log(res.toString()); // First(true)

[JS Compose] 5. Create types with Semigroups的更多相关文章

  1. Node.js NPM Tutorial: Create, Publish, Extend & Manage

    A module in Node.js is a logical encapsulation of code in a single unit. It's always a good programm ...

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

  3. [JS] Topic - Object.create vs new

    故事背景 Ref: 你不知道的javascript之Object.create 和new区别 var Base = function () {} (1) var o1 = new Base(); (2 ...

  4. js创建对象 object.create()用法

    Object.create()方法是ECMAScript 5中新增的方法,这个方法用于创建一个新对象.被创建的对象继承另一个对象的原型,在创建新对象时可以指定一些属性. 语法: Object.crea ...

  5. [Compose] 19. Leapfrogging types with Traversable

    We use the traversable instance on List to reimplement Promise.all() type functionality. For example ...

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

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

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

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

随机推荐

  1. 使用Notepad++的XML Tools插件格式化XML文件

    转自“”:https://blog.csdn.net/qq_36279445/article/details/79803310 1. 安装XML Tools插件 (1) 通过网址http://sour ...

  2. IIS特殊字符设置

    简介:[iis7]请求筛选模块被配置为拒绝包含双重转义序列的请求.HTTP 错误 404.11 - Not Found 特殊字符最好替换成其他的字符,主要的特殊字符有”*”.”&”.”%”.” ...

  3. Logstash之Logstash inputs(file和redis插件)、Logstash outputs(elasticsearch 和redis插件)和Filter plugins

     前期博客 Logstash安装和设置(图文详解)(多节点的ELK集群安装在一个节点就好) Filebeat啊,根据input来监控数据,根据output来使用数据!!! 请移步, Filebeat之 ...

  4. golang sync.Mutex

    //go func 和主线程之间的关系是并行和竞争关系 package main import ( "fmt" "sync" "time" ...

  5. Vue的全选功能实现思路

    全选功能的实现主要分两步: 1. 点击全选框选中所有选择框. 2. 当所有选择框都被选中时,勾选全选框. 详细思路: 1. 点击全选框选中所有选择框: 给全选框绑定一个值,然后添加change时间,当 ...

  6. IOIOI卡片占卜(Atcoder-IOIOI カード占い)(最短路)

    题目描述: K 理事長は占いが好きで,いつも様々な占いをしている.今日は,表の面に ‘I’ が,裏の面に ‘O’ が書か れたカードを使って今年の IOI での日本選手団の出来を占うことにした. 占い ...

  7. Dao层封装泛型实现(spring mvc,springjdbctemplate)

    代码片段(6) [全屏查看所有代码] 1. [代码]BaseDao     跳至 [1] [2] [3] [4] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

  8. js检查元素是否包括在数组中

    说明 在系统中须要检查税率填写的正确性,一定是国家规定的某几种税率,当然能够通过if else进行校验,可是还能够使用定义一个数组然后校验是否包括在元素中进行校验. 长处:加入税率无需改动逻辑,仅仅须 ...

  9. Binary Search Algorithm

    二分查找代码: //============================================================================ // Name : Bin ...

  10. Django的命令

    安装django          : pip install django 创建django项目   :django-admin startproject projectname 启动django项 ...