DEV Community

Discussion on: Grokking the Reader Monad

Collapse
 
ducaale profile image
Mohamed Dahir

Hi, thanks again for this series on Functional Programming.

  • I was wondering what is the replacement for inferred inheritance in other languages i.e Haskell?
  • I couldn't help but see the similarity of Reader Monad with State monad. Is that something you might include in this series?

Couple of typos I noticed while trying to wrap my head around this article 😅

  1. In Uniting dependencies 👭, UserId -> float ('deps -> unit) should be UserId -> float -> ('deps -> unit).
  2. In A final improvement section, wrapping the implementation in reader { } should be wrapping the implementation in injector { }.
  3. In the first code snippet of Uniting dependencies 👭 , one of the functions has = sign at the end while the other one doesn't.
module Database =
    let getUser (UserId id) (connection: #ISqlConnection) : User =

module PaymentProvider =
    let chargeCard (card: CreditCard) amount (client: #IPaymentClient): TransactionId
Enter fullscreen mode Exit fullscreen mode
Collapse
 
choc13 profile image
Matt Thornton • Edited

Thanks for pointing out those typos, my editorial team obviously missed them 😂

You’re right to spot the similarity to the State monad. I believe the State monad is basically the Reader + Writer monads. So the State monad allows you to modify the state too (by creating a new copy with the modifications) as well as read from it. So yeah if we combine Reader + Writer then we get State.

On the inferred inheritance point then I believe in Haskell it would be done with type classes. I believe this is more commonly referred to as the MTL style. I’ve not done any large scale Haskell projects so I wouldn’t want to give an example here as I suspect I’ll make a mistake, but if you look into type classes and MTL then I think that will give you the answer.

I also plan to do more on how to achieve IoC in FP later in this series after I’ve introduced a few more fundamentals. In that I’ll try and provide a more production ready version of inferred inheritance and also some other ways of grouping dependencies together. As just using it like I’ve introduced it here probably won’t scale too well for large code bases. That’s not to say it’s not a useful technique it just requires a bit of a longer treatment than I could fit into this post.