3 ms·
Please explain a parser monad in terms of flat map. To be concrete, I'm talking about the kind with the semantics that if `Parser X` parses an `X`, and `Parser
by unanswered 5y ago
Please explain a parser monad in terms of flat map.
To be concrete, I'm talking about the kind with the semantics that if `Parser X` parses an `X`, and `Parser Y` parses a `Y`, `>>` between them parses an X then a Y. I propose that you must explain that in terms of flatMap in order to say that this is a correct understanding of monads.
- BoiledCabbage 5y agoSure. flatMap() is a specific function that you already understand. It is the most common instance of a general fn pattern that is used for the specific data type Array. The flatMap equivalent function when used on the data type Promise is called then(). The special function you are asking to understand ">>" is simply another instance of then(). This one still waits for the promise to complete - the only difference is that the lambda you pass to then() doesn't have access to the result of the previous promise. If you're using >>/flatMap on a Parser datatype, instead of running the lambda after first promise has completed successfully, it "runs" it after the first parser has completed successfully. flatMap and (all of its other data type specific names) are the essence of a Monad.
- dragonwriter 5y ago> flatMap and (all of its other data type specific names) are the essence of a Monad. Well, the extra sauce on top of an Applicative Functor that makes a Monad; but the essence of an Applicative Functor is also part of the essence of a Monad, and the essence of a plain Functor is part of the essence of an Applicative.
- unanswered 5y agoNot only did this 'answer' not reference any specific properties of flatMap; it didn't reference any specific properties of parsers either. I hold it wholly insufficient and incorrect.