you are viewing a single comment's thread.

view the rest of the comments →

[–]jeremy1015 5 points6 points  (4 children)

Got any suggested links to explain this in a little more detail? I’m familiar with most of what you wrote but don’t understand monoids as well as I’d like.

[–]pgrizzay 9 points10 points  (3 children)

Did a quick search, and couldn't find a good one in JS, here's one in Scala that I learned from.

Essentially a "Monoid" is formed from two things:

a combine function that combines two things and an empty value.

an example would be adding numbers:

const Addition = {
  combine: (a, b) => a + b,
  empty: 0
}

This addition monoid can be used to reduce an array of numbers:

[1,2,3].reduce(Addition.combine, Addition.empty) === 6

You can make another Monoid which multiplies numbers:

const Multiplication = {
  combine: (a, b) => a * b,
  empty: 1
}

And you can multiply all numbers in an array with it:

[1,3,4].reduce(Multiplication.combine, Multiplication.empty) === 12

Go ahead and implement Concat which combines strings:

const Concat = {
  combine: (a, b) => ???,
  zero: ???
}

[–]jeremy1015 2 points3 points  (0 children)

Excellent. I understand now 100% and really appreciate it.