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. 中间件 —— 消息中间件(MOM)

    维基百科对消息中间件的定义为:Message-oriented middleware (MOM) is software or hardware infrastructure supporting s ...

  2. golang API 例子实现

    golang API 例子实现 http://files.cnblogs.com/files/rojas/astaxie.zip

  3. 微信小程序仿微信运动步数排行-交互

    效果图如下: 图片.png wxml: <view class="item-box"> <view class="items"> < ...

  4. 编码与乱码(05)---GBK与UTF-8之间的转换--转载

    原文地址:http://www.blogjava.net/pengpenglin/archive/2010/02/22/313669.html [GBK转UTF-8] 在很多论坛.网上经常有网友问“  ...

  5. query中prop()方法和attr()方法的区别

    query1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 官方例举的例子感觉和attr()差不多,也不知道有什么区别,既然有了prop ...

  6. C# 映射

    public class Myclass1 { private int m_Count = 100; public string love{get;set;} public int Count { g ...

  7. 10559 - Blocks(方块消除|DP)

    该题乍一看和矩阵链乘非常类似,但是有一个不同之处就是该题能够拼接 .   为了达到这个目的.我们不得不拓展维度d[i][j][k].用一个k表示最右边拼接了k个和a[j]同样颜色的方块. 问题的关键在 ...

  8. javascript的组成

    ECMAScript:(3/5/6/7) 它是JS语言的标准,规定了JS的编程语法和基础核心知识. DOM:document object model 文档对象模型,提供给JS很多的操作页面中元素的属 ...

  9. JS学习笔记 - fgm练习 2-5 - 函数传参 设置div样式

    练习地址:http://www.fgm.cc/learn/lesson2/05.html <script> window.onload = function(){ var oDiv = d ...

  10. 11.3 Android显示系统框架_最简单的surface测试程序

    APP有一个surface(界面),其有多个buffer用来存放界面数据,这些buffer是向surfaceflinger申请的: 因此我们编写的surface测试程序步骤: (1)获得surface ...