Semigroup examples

Brian Lonsdorf
InstructorBrian Lonsdorf

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

A few examples of Semigroup definitions

[00:00] Suppose we have a game and one of our players, Niko here, wants to merge their accounts. They've accidentally made two accounts. What we can do is think about how are we going to combine these two things? Whenever we think of combining we should be thinking semigroup, because that is a way to concat two things or combine two things together.

[00:15] Now, one of the properties we can exploit is if a data structure is entirely made up of semigroups it will be a semigroup itself. If I can concat all the pieces of my data structure then my data structure is therefore concatable.

[00:27] We want to concat these two objects together and what we can do here is say, instead of concating the name into a string, NikoNiko -- that would be kind of strange -- why don't we go ahead and change the semigroup to first, here around the name, so it knows how to combine it correctly?

[00:44] Then, with isPaid, I'd like to combine these two bullions. If they're both paid or if all of them are paid then we're paid. Let's do that. Let's say all. That will keep us in good standing. No cheating there. Points will be just the sum of the points.

[00:58] As it happens, these friends here are just an array so it will already be able to concat on that. It's a semigroup there. We can go ahead and write, "Constant results" is the account one concated with an account two here. This isn't going to quite work yet because objects do not have a concat method on them.

[01:18] What I'll do here is I brought in my own library of immutable extensions. It works like Immutable JS with some extra little flair on the end of it. This is your custom library, but I wouldn't necessarily call it custom.

[01:28] This is just a reimplementation of the same things you'll find in Scala or Haskell or what have you. They're just generic data structures. But anyway, where were we? Here we have a map, objects here, or a map type that will define concat exactly like we like it.

[01:41] Now we can [inaudible] that log our results and see that we've gone ahead and concated both together. Oh, we have to call 2 JS on this. It's Immutable JS, you have to do these things.

[01:52] There we go. Now you can see that the name has kept the first part. It is paid if all of it is paid. The sum of the points is there and all of our friends have been carried over.

~ 5 years ago

You mention you use a special immutable library that has some flare added. Are you talking about or other?

Brian Lonsdorf
Brian Lonsdorfinstructor
~ 5 years ago

Hi! Posting here for everyone:

This library is not "my" ideas. Just a JS port of normal types from FP langs. Same functions will live in other FP libs which implement Map/List

~ 4 years ago

I tried to make last concat work using just vanillaJS (no 'require' from NodeJS) and failed; I checked and there is no export in this library so I cannot import Map (but he's just altering existing Map so maybe I don't have to); I tried to use this library both by linking from url and from local file and I still get:

Uncaught TypeError: Constructor Map requires 'new' at Map (<anonymous>) at script.js:19

Any advice on this?