[JS Compose] 5. Create types with Semigroups

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(1).concat(Sum(2));
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)
时间: 2024-10-10 17:45:44

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

[JS Compose] 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 type we call Box. This is our introduction to working with the various container-style types. At first, might not be comforable with 'Box' or 'Contai

[Compose] 19. Leapfrogging types with Traversable

We use the traversable instance on List to reimplement Promise.all() type functionality. For example we want to conver: [Task] => Task([]) Conver array of Task, into Task of array value. To do that we can use traverse. const fs = require('fs') const

[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: 10, friends: ['Franklin'] } const acct2 = { name: 'Nico', isPaid: false, points: 2, friends: ['Gatsby'] } So, here we can use Semi

[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 so it's not a safe operation, whereas with the monoids we could take as many as we possibly want, even none, and still return us back something. It's a

[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-nested expression. For example, we have code: const moneyToFloat = str => { const cost = str.replace(/\$/g, ''); return parseFloat(cost); } const percentT

[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 introduce the chain function to deal with nested Eithers resulting from two try/catch calls. For example we have this code using try & catch: const getPo

[JS Compose] 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 our code. Now, we try to make Box more useful. We want to do a force null check by define "Right" and "Left" tow boxes. What "Right

Analytics.js简介

analytics.js JavaScript代码段是一种可用于衡量用户与您网站的互动情况的全新方式.它与之前的跟踪代码ga.js类似,但为开发者自定义实现方案提供了更大的灵活性. analytics.js代码段是Universal Analytics(目前处于公测阶段)的一部分.新用户应使用analytics.js.现有的ga.js用户应为analytics.js创建新的网络媒体资源并在他们的网站中进行两次代码实现.在同一页面上添加ga.js和analytics.js代码段并不会有任何安全问题

【js】为什么要使用react+redux

前端的浪潮一叠叠袭来,带走了jQuery,带走了backbone,带来了react,带来了redux,但是面对层出不穷的前端技术,我们应该何去何从呢?近一年来笔者的也发生了同样的变化,技术栈从.net+backbone+requirejs+grunt变成了nodejs+react+webpack+gulp,一系列的变化也让笔者对整个过程,整个闭环的工具链有了一些自己的感受和理解,于是有了今天此文. 其实react出现得很早,但是笔者所在的唤作"大象转身"的大公司,所以内部技术的迭代,并