Creating Semigroup Data Types from Scratch

Thomas Greco
InstructorThomas Greco
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Semi-groups are an important part of functional programming. They're not quite monoids, however, they provide us with the foundation we need to create monoids.

In this video, we'll learn about semigroups and see how they provide us with a generic interface for combining values using a .concat method. By the end of the lesson, you will have learned about the properties that a type must possess in order to be considered a valid semigroup.

Fantasyland Docs - Semigroup

Thomas Greco: [0:00] Semi-groups provide us with an interface for combining one or more values together. Here, I'm just creating a semi-group called Sum. The first thing we'll do is just set the value being passed in. The next thing we need to do is add a concat function. All semi-groups need to have this concat function.

[0:31] What this is going to do is it's going to take in another semi-group and in this instance, it's going to use addition to combine both these values. Keep in mind that we're wrapping this return value in the Sum, as this will allow us to continue concatenating values for as long as we need.

[0:54] [inaudible] console.log statement, and if we have a Sum(10) and we concatenate that with the Sum(20), we should see that we get 30 as our result. If I just execute this code...Remove these statements. If I execute it, I see that we get that value of 30 inside of our object.

egghead
egghead
~ 2 hours ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today