Re:Monads are like burritos (2009)
by aodonnell2536 on Sunday June 02, 2024 @04:17PM
(#40556929)
Would it then follow that commands/builtins like `time` and `sudo` are monads? What about shell commands in general, since you can always chain together stdout to stdin with pipes?
Re:Monads are like burritos (2009)
by chuckadams on Sunday June 02, 2024 @04:37PM
(#40557056)
Streams are definitely monads, so if you're reading lines from stdin and printing zero or more lines for each to stdout, you basically have a List monad (Haskell's laziness makes every list a poor man's stream, though if you're doing serious streaming work, you'd probably want something more specialized).
Mind you, shell pipes aren't actually implemented monadically, but you can certainly visualize it that way when working with functional libraries.
"Monads embody the concept of "chaining" functions."
No, it doesn't. Monad implementations may call the provided function zero times, or multiple times. Chaining implies exactly once, one of the common misconceptions about the monad interface.
Flatmap is another common attractive nuisance; flatmap is the monad implementation for lists but is not "monad" in general, any more than "an iterator on linked lists" is "iterator" in general.
Re:Monads are like burritos (2009)
by chuckadams on Sunday June 02, 2024 @08:17PM
(#40558194)
Chaining implies sticking one function to another, with the implication that some data is being threaded through them.
Whatever extra semantics go on top of that are gravy, it's a vague term for a vague concept. I even gave examples where the function might be invoked zero times (Maybe) or multiple times (List). And in some languages (I specifically mention one) flatmap is a function on all monads, not just lists.
If you understand it correctly, then use correct terms to describe it. And I don't mean the mathematically correct pristine words that some particular FP language uses, I mean, terms that will create correct impressions in the audience and not incorrect ones. You don't help by using the most commonly misunderstood terms and reinforcing the widespread misconceptions about how it works. Both "flatmap" and "chaining" create completely wrong impressions. You just leave people having to clear out the misunderstandings to get to the real and not really that complicated truth.
Re:Monads are like burritos (2009)
by chuckadams on Monday June 03, 2024 @10:55AM
(#40563227)
Feel free to jump in with your corrections or suggestions for the actual material then instead of sniping at my terminology. I'm done.
Again, the cool thing is that it's the same syntax no matter what kind of Monad you're in, so you can totally change the behavior of a monadic function by just using it with a more specific return type (at least in Haskell, other languages might make you pass around an implementation) Monads are a neat tool of composition, but composing monads themselves is fiddly and cumbersome, involving monad transformer "stacks" of deeply nested generic types. Monad transformers are really just not fun.
I'd give specific examples, but I'm kind of lethargic from the burrito I had for lunch. If you're familiar with flatMap(), a Monad is anything that's "flatmappable" (which in JS is just arrays, but languages like Scala take it much further). In C#, it's IQueryable (LINQ is a monad!)