News for hackers. Stuff that matters.
Re:If monads are the solution, what is the problem? by jerf on Monday July 30, 2018 @03:17PM (#17647151)
'So it's a wrapper that implements a particular interface? Then why, oh why, do we have so many people who spout, "A monad is just a monoid in the category of endofunctors, what's the problem?"'

"Monad" seems to be just beyond the complexity event horizon where it is difficult for people to get a really clean idea of what it is, so instead we've got dozens of ideas of what they are floating around, probably a good two-thirds of them with fundamental errors in them, which then contributes to the general haze surrounding the ideas.

I've got a mostly-written blog post on deck with the tentative title "Functors and Monads For People Who Have Read Too Many Tutorials", for which around 80% of it isn't explaining what they are, but systematically walking through all the errors I've seen (some of which are in the type notation for Haskell itself; the "[a]" abbreviation for what should be "[] a", if not "List a", alone has done untold damage to comprehension) before finally getting down to what the things are.

(Functor in particular is so trivial it's hard to believe it's that trivial when you finally really get it. Monad legitimately has a bit of a twist in it, and some of the specific uses of that interface take that twist and hammer it home to produce some very funky code, so the fact that it's a bit foggy isn't that big a surprise. The fog that functor has picked up by association with monad is much, much larger than the concept itself, though.)

"So it's a way of imperatively declaring execution sequence dependencies that lets you reason as if there's only pure functions most of the time."

It turns out no, that's just one use of them. A very big one, a very natural one, and the one that drove their prominence in the current debate... but it's not actually what the interface is "about" per se. For a good counterexample, consider the parsing monad implementations that "declaratively" [1] create a parser. It's not really about declaring execution sequences.

(There's also a non-trivial amount of confusion that arises from the fact that monad and laziness have a lot of interaction. The monad interface in a strict language would be used differently than it can be in Haskell.)

Re:If monads are the solution, what is the problem? by agentultra on Monday July 30, 2018 @04:57PM (#17647957)
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.

Re:If monads are the solution, what is the problem? by pwm on Monday July 30, 2018 @06:14PM (#17648638)
Let 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.
Re:If monads are the solution, what is the problem? by red75prime on Tuesday July 31, 2018 @03:09AM (#17651142)
I 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.

Re:If monads are the solution, what is the problem? by Serow225 on Monday July 30, 2018 @04:00PM (#17647494)
I would love to read that post, the title describes exactly the boat I'm in :) how can I subscribe so I don't miss it?
Re:If monads are the solution, what is the problem? by jerf on Tuesday July 31, 2018 @08:44AM (#17652676)