Everyone talks about monads but monoids are where it's at. Monoids are simple and make distributed computation a breeze. In this episode, we learn the two properties that make an operation a monoid, and how to use it do distributed computation.
►► Audio, Video, and Transcript available: [ Ссылка ]
►► Subscribe on iTunes: [ Ссылка ]
Transcript
What is a monoid? By the end of this episode you will understand the two properties of monoids, and you'll be able to apply them in your systems. That's a big promise. My name is Eric Normand and I help people thrive with functional programming.
Why are monoids important? Why even bring them up? That's a really good question.
I know a lot of people talk about monads. I think it's mostly because they want to know what they are and they don't understand them. They think there's some magic in Haskell about how IO happens with monads. That's another topic.
This episode is about monoids. I actually think monoids are more interesting, more applicable to helping us write better code. Especially in a parallel or distributed code in the system that's got a computation that has to spread out over different course or different machines.
A monoid lets you break up a task into smaller tasks pretty much arbitrarily. You don't have to spend a lot of computation figuring out how to break it up. You break it up into small tasks, spread that out to different workers. These are on different threads or in different machines. Here's the key. The monoid lets you put them back together. Put the answer back together.
What are the two properties of monoid? The two properties of a monoid...First of all, it's an operation. A monoid is an operation. It's not a type or a kind of type. It's not a property of a certain class of values. It's a property of an operation over those values. For example, addition of numbers is a monoid. Multiplication of numbers is also a monoid.
What are the two properties that operation is associative? I have a whole episode on what is associative. I've explained it before. I'll explain it briefly in a second. The other property is that operation needs to have an identity value. I also have an episode on that. You should look those up if this is confusing. I'll go over a brief recap.
An associative operation is one that has two arguments, otherwise known as a binary operation. Addition has two arguments. It has A plus B. A and B are the two arguments. That's pretty clear. It's got to have two arguments.
Here's the other thing. It's got to take values of the same type and return a value of that same type. All three things, the two arguments and the return value have the same type.
Look at numbers, takes two numbers, returns a number. That's addition, right? Multiplication is the same way. Takes two numbers and returns a number. It's not returning a string or something else. It's returning a number.
That's part of what makes it associative. The other thing is that the grouping. Because you've got this...I look at it like a triangle. Like an upside down triangle. Got an A and a B and it gives me a C and they're all the same type.
Because I've got the A and the B at the top and the C at the point at the bottom, you can think of this associative operation as a combining operation. Addition is a way of combining two values.
It's not always combining. It's not always clear that that's a good way to think about it. In multiplication, are you really combining two numbers? With addition, you definitely are. You're combining two piles of rocks into one big pile of rocks. That's addition. It's where the abstract operation of addition comes from. If you just take a piles of things and you put them together and now you have a big pile.
If you imagine, I have three things to add. I have A, B and C and I want to add them up. I can add up A and B first and get a number and then I add C to that number, so I use the same operation again. I did a plus, A plus B, get a number, let's call it D and then I take that D and I do D plus C and now I get E and that is my answer.
You notice it's...I'm trying to make it graphical here, I add A and B and those are in a triangle and lead down to the point D. Then that D and C form a new triangle and it leads down to the point E.
Ещё видео!