Looking at the title of section 10.6 (Handling state with Monads), I thought State monad will be discussed. The first 5 paragraphs in this section strongly suggest that the intention is to discuss the idea of mutable state as implemented in pure-functional languages--via a state monad. However, it looks like the examples are of the Writer monad. As the name "Writer monad" won’t say much to a C++ programmer, could this section be called something else? Like Log-Appending monad. That’s not catchy though.
I think this section should not confuse with state monad as the computation is NOT dependent on existence of a state. Usage of an empty std::string in the constructor of with_log confirms that a monoid is used.
Also val.log() should not be passed to the function f. Instead, val.value() should be passed.
This discussion appears relevant: https://stackoverflow.com/a/22140063
|