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. Detecting a return-oriented programming exploit

    A method and apparatus for detecting a Return-Oriented Programming exploitation. At a computer devic ...

  2. oracle 归档模式和非归档模式

    http://www.cnblogs.com/gaojian/p/3611641.html http://blog.csdn.net/yong5241200/article/details/39451 ...

  3. 5.9 enum--支持枚举类型

    enum模块提供了枚举类型的支持.枚举类型是由一个名称和一个统一值来组成.值是常量的值.它们之间能够通过名称进行比較和引用,还能够迭代訪问. 5.9.1 模块内容 本模块主要定义了两种枚举类型:Enu ...

  4. 第三次作业 201731082208 黄亚恒&肖莉

    Github项目地址:https://github.com/HYHSTUDEY/WordCount.git 作业地址:https://www.cnblogs.com/hyhhyh090628/p/10 ...

  5. Android 6.0 最简单的权限获取方法 RxPermition EasyPermition

    Android 6.0 要单独的获取权限 这里提供两种很简单的方法 EasyPermition RxPermition EasyPermition https://github.com/googles ...

  6. loadrunner--设置代理录制

    为了解决loadrunner录制不到脚本或录制经常无响应等情况,可以选用代理录制方式,步骤如下. 打开配置好代理的浏览器,输入要录制的服务器ip,开始操作,录制完成后点击Shutdown

  7. 【习题 6-5 UVA-1600】Patrol Robot

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设dis[x][y][z]表示到(x,y)连续走了z个墙的最短路 bfs一下就ok [代码] /* 1.Shoud it use l ...

  8. Android Studio - no debuggable applications 的解决的方法

    之前logcat总是无法显示调试应用的信息 曾经我都是卸载重装.后来发如今StackOverflow有一个哥们说的非常对.一次就成功. 原话是这么说的: You also should have To ...

  9. 【CS Round #48 (Div. 2 only)】Game of Chance

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...

  10. ex.Message "ORA-01691: Lob 段 USER_MURPHY.SYS_LOB0000093717C00006$$ 无法通过 1024 (在表空间 ZJHH 中) 扩展"

    Oracle,往数据库里导入dmp的时候报错:ORA-01691:Lob 段 无法通过8192(在表空间TS_SI中)扩展 解决方案1: 原因:所创建的表空间不足. 创建一个可拓展的表空间 creat ...