Hacker News new | past | comments | ask | show | jobs | submit login

Haskell has the IO Monad which is like a magic type that is allow to have any side effect whatsoever. So you are free to write imperative code in Haskell.

However what you might want to do (and this is based on IIRC!) is have a typeclass for your database interaction. Then the real DB type and the mock one can both implement that typeclass and you have basically dependency injection.

There are other ways like free monads but I never got my head around that.

You could also create a monad transformer which is a bit more straight forward. They let you compose monads, so you can chain little bits of side effect. Like “here is something that can talk to a database and write logs and that is all it will do” … but actually it is pure! It thinks it is doing those things but they can be mocked.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: