Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> why you'd want to do this is another question. (And one I'd be happy to answer, if you're curious.)

If you would, please.



Sure! Here's a couple concrete applications:

- Any monoid operation is trivially parallelizable: take the dataset, split it into chunks, combine all the elements in each chunk together, then combine those results together as a final step.

- If I'm updating a row in my database with a monoid operation, I can always 'pre-aggregate' a batch of values together on the client side before taking that result and combining it with the stored value.

- If I store some statistics for every day of the year, I can calculate the monthly statistics very cheaply -- as long as those stats are a monoid.

The monoid abstraction seems weird at first, because it's so dang general, but it ends up hitting a bit of a sweet spot: the rules are just strong enough to be useful for a bunch of things, but simple enough that be applied to all kinds of different situations. You can think of it kind of like a hub-and-spokes thing -- this interface connects all kinds of different data types to all kinds of different cool situations, so you get a lot of functionality with a lot less typing and thinking.


Another answer as to "why" is to unask the question.

You don't "construct" monoids intentionally, you instead realize that types you already have are monoids under the covers.

Realizing this structure and accentuating it is a good way to discover and enforce good program structure.


The introduction of stream processing in Java 8 is a concrete example - it can auto-parallelize computations. It was not immediately clear to me why an identity value was needed in this function:

    http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-T-java.util.function.BinaryOperator-
After reading up on monoids, it became clear. Note how the reduce() variant that does not take an identity value is allowed to return null!


A recent post, was about "How to Build Your Distributed Database" (1). The word 'monoid' is not used (and the word 'commutative' is even misused for 'associative') but this post shows well why monoids matter and how they can be used.

(1) https://news.ycombinator.com/item?id=9101971




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: