2025-01-11

Reader Monad

Notes
This article was translated by GPT-5.2-Codex. The original is here.

This time I looked at the Reader monad and implemented the sample from the Reader monad page of All About Monads using Cats. I just started using Cats so there may be a better way to write it, but it works for now, so I’ll just post the code.

I think Cats MTL could make some parts more readable.

Compared to the Haskell code, the following points were different.

  • Haskell's asks corresponds to ReaderT.ask[Id, _].map(...) in Scala
    • There is no equivalent to asks (maybe?)
  • Haskell's mapM corresponds to traverse in Scala
  • Type inference doesn't work as well as in Haskell (maybe my style is not great) 😢

Since I rarely write this kind of code after starting work, I can’t help but think that procedural code might be clearer. So I plan to implement the same logic without the Reader monad and compare.