3 ms·
Functor, Monoid, and Semigroup are the worst. There's a complete lack of literacy when it comes to abstract algebra in practical programming. Monoid is so incre
by agentultra 8y ago
Functor, Monoid, and Semigroup are the worst. There's a complete lack of literacy when it comes to abstract algebra in practical programming. Monoid is so incredibly simple but the jargon and terminology is what makes them scary to newcomers.
Monoid: A data structure wrapping a value of some type that implements the Monoid interface. The interface has two methods: `identity` and `append`. If you implement those methods for your structure and your implementation follows particular rules then... you have a Monoid.
Why are they cool? You can append a lot of things together; not just lists. You now have a generic, binary append operator `mappend` that can join together anything that implements the Monoid interface.
What can you use it for? Well.. lists for a start. Configuration? Sure! Load up configs from the shell environment, the `~/.myapp` file, and the `/etc/myapp` file; concatenate them together with your generic `mappend`... boom. Streams? Sure. Trees? Why not. If you can implement the interface you get the operators. You code can stop caring whether it's a this or that. It can focus more on do this, do that.
Semigroup: a monoid without the `identity` method in its interface.
Functors, also a shockingly simple concept hidden behind jargon.
- pwm 8y agoLet me try Functor: You have a structure of values of some type. You want to apply a function to these values inside the structure, but you don't know how to do that. Luckily the structure itself does, so you just give your function to the structure and it applies it to its values for you. Structures that know how to apply functions to its values are called Functors.
- red75prime 8y agoI don't think it is jargon that makes understanding difficult. It is efforts required to develop a sense or intuition for those concepts. Like when you are looking at python list comprehensions and see list monad, looking at Go's `if err != nil then return err` and see Either monad and so on. I've developed such intuition twice and it evaporated both times, when I wasn't using it for long enough.