Live data from Hacker News

Fold-... and Monoids

funcall.blogspot.com

31–40 of 48 posts

Re: Fold-... and Monoids

#32
post #8

Earlier quoted context omitted.

I found it useful to have them mentioned, since people new to this topic (or, e.g., Haskell) tend to bump onto monoids when they first try to understand monads. A 'handwavy' association that somewhat makes sense and allows you to have some sort of perspective when moving on to monads is better than simply omitting the link to monads completely, just because one can "kindof maybe" find holes in the simplified explanat…

> tend to bump onto monoids when they first try to understand monads That's unfortunate. They should be bumping onto monoids much earlier, and much more often. Yeah, IO and do notation put monads on the face of people way before they have time to adapt to it. But monoids are the one that are extremely valuable, simple, and easy to learn. Also, they make for a nice step in a progressive adaptation to the "generalized…

Why? Serious question, but what's the use of monoids? I encountered the term years ago, when I had an ill-fated ambition to make sense of monads. I've let it go and made peace with the world. But outside that narrow context, I've never even heard the term "monoid". What are people using it for in the real world?

Re: Fold-... and Monoids

#33
What's particularly interesting is that folds are not limited to processing lists. For any recursive data structure, you can create a corresponding fold. What's even more interesting is that you can organize your code to automatically create the corresponding fold from a given recursive data structure!

Here's one example I wrote up using trees as the data structure:

https://github.com/tmoertel/practice/blob/master/EPI/09/soln...

Here's another example, this one in Python: https://github.com/tmoertel/practice/blob/master/dailycoding...

Re: Fold-... and Monoids

#34

Earlier quoted context omitted.

> tend to bump onto monoids when they first try to understand monads That's unfortunate. They should be bumping onto monoids much earlier, and much more often. Yeah, IO and do notation put monads on the face of people way before they have time to adapt to it. But monoids are the one that are extremely valuable, simple, and easy to learn. Also, they make for a nice step in a progressive adaptation to the "generalized…

Why? Serious question, but what's the use of monoids? I encountered the term years ago, when I had an ill-fated ambition to make sense of monads. I've let it go and made peace with the world. But outside that narrow context, I've never even heard the term "monoid". What are people using it for in the real world?

As the article explains, fold functions are a simpler way to think about monoids. So in recursive pure functional programming, many important iterative processes (pipelines, accumulators, machine states...) can be expressed as an application of fold-l or fold-r.

Re: Fold-... and Monoids

#35

Earlier quoted context omitted.

> tend to bump onto monoids when they first try to understand monads That's unfortunate. They should be bumping onto monoids much earlier, and much more often. Yeah, IO and do notation put monads on the face of people way before they have time to adapt to it. But monoids are the one that are extremely valuable, simple, and easy to learn. Also, they make for a nice step in a progressive adaptation to the "generalized…

Why? Serious question, but what's the use of monoids? I encountered the term years ago, when I had an ill-fated ambition to make sense of monads. I've let it go and made peace with the world. But outside that narrow context, I've never even heard the term "monoid". What are people using it for in the real world?

Roughly, something is a monoid exactly when a parallel reduce type of algorithm can be used. The associativity lets you break it into sub-problems, and the unit lets you insert padding where necessary to get same-sized blocks for parallel processors. It's also a useful concept to know for library design. e.g. when there's a "combine" or "reduce" operation on some data type, it should occur to you that your users will probably want a neutral "do-nothing" element and that your operation should give you a monoid. APIs without one are usually annoying to work with and require extra if statements/special casing.

More generically, named concepts like this give you a way to compress knowledge, which makes it easier to learn new things in the future. You get comfortable with the idea of a monoid, and when you meet a new one in the future, you immediately have an intuitive ground to build on to understand how your new thing behaves.

Re: Fold-... and Monoids

#36

What's particularly interesting is that folds are not limited to processing lists. For any recursive data structure, you can create a corresponding fold. What's even more interesting is that you can organize your code to automatically create the corresponding fold from a given recursive data structure! Here's one example I wrote up using trees as the data structure: https://github.com/tmoertel/practice/blob/master/EP…

Yes, that is because folds work on catamorphisms in category theory.

Re: Fold-... and Monoids

#37

Earlier quoted context omitted.

It's kind of natural that you need to progress from a magma/semi group monoid (algebra) to functors/applicative/monad (category theory) Would it help if you defined a monoid as a combination of 3 things? 1) a data type A 2) an associative operation on A 3) an identity (or empty element) Then you can correctly say that the string data type, admits an associative operation (concatenation of two strings) and you have an…

> Haskell developers incorrectly assume that you can only have one semi group (or equality, monoid, etc) instances for your data type They don't assume that. The devs bent the compiler backwards several times trying to support more than one instance, but they still couldn't design an implementation that is actually good to use. If you know of any language where this works well, it would be nice to know. AFAIK, repres…

Scala/Typescript

Re: Fold-... and Monoids

#38
post #8

Earlier quoted context omitted.

I found it useful to have them mentioned, since people new to this topic (or, e.g., Haskell) tend to bump onto monoids when they first try to understand monads. A 'handwavy' association that somewhat makes sense and allows you to have some sort of perspective when moving on to monads is better than simply omitting the link to monads completely, just because one can "kindof maybe" find holes in the simplified explanat…

> tend to bump onto monoids when they first try to understand monads That's unfortunate. They should be bumping onto monoids much earlier, and much more often. Yeah, IO and do notation put monads on the face of people way before they have time to adapt to it. But monoids are the one that are extremely valuable, simple, and easy to learn. Also, they make for a nice step in a progressive adaptation to the "generalized…

In the world that I imagine could exist, we'd do away with algebra 2 and pre-calculus in high school, which are a waste of 2 years, and instead do something like algebra -> geometry -> calc1 -> calc2 -> linear algebra -> abstract algebra, with linear algebra being concrete things like adding arrows and solving systems, and abstract algebra introducing basics of monoids, groups, vector spaces, and homomorphisms. It's sort of unfortunate that even the basic ideas of algebraic thinking (i.e. structures and structural transformations) are pretty much not even hinted at to anyone but math majors, and yet we spend years of school on something called "algebra". So even technical people can't see the point of structural modeling.

Re: Fold-... and Monoids

#39

What's particularly interesting is that folds are not limited to processing lists. For any recursive data structure, you can create a corresponding fold. What's even more interesting is that you can organize your code to automatically create the corresponding fold from a given recursive data structure! Here's one example I wrote up using trees as the data structure: https://github.com/tmoertel/practice/blob/master/EP…

Yes, that is because folds work on catamorphisms in category theory.

Indeed! The first example I linked to explains this connection in detail.

Re: Fold-... and Monoids

#40

Earlier quoted context omitted.

> tend to bump onto monoids when they first try to understand monads That's unfortunate. They should be bumping onto monoids much earlier, and much more often. Yeah, IO and do notation put monads on the face of people way before they have time to adapt to it. But monoids are the one that are extremely valuable, simple, and easy to learn. Also, they make for a nice step in a progressive adaptation to the "generalized…

Why? Serious question, but what's the use of monoids? I encountered the term years ago, when I had an ill-fated ambition to make sense of monads. I've let it go and made peace with the world. But outside that narrow context, I've never even heard the term "monoid". What are people using it for in the real world?

It's a type of nice structure. Lists with concat, strings with append, etc. "Friendly chunkability", if you like. For instance, map reduce is a monoid homomorphism - when I see 'monoid homomorphism' in the wild, I think 'parallelizable' etc. It's a handy concept.
Post reply on HN