4 ms·
Use IO for everything at the beginning. Use wai/warp to run an app directly and setup handlers. Use postgresql-simple to send queries. Read the settings object
by spion 10y ago
Use IO for everything at the beginning. Use wai/warp to run an app directly and setup handlers. Use postgresql-simple to send queries. Read the settings object once, then pass it everywhere.
You can slowly start writing generic types for your pure code after a while. For example, replace types such as `[t]` with `Functor f => f t` if the only list-related operation done in that function is `map` (fmap). Try to take advantage of the stuff here: https://wiki.haskell.org/Typeclassopedia https://wiki.haskell.org/Typeclassopedia
After a while of doing this, you can start looking into how you can write your code in a way to have more generic types, then specialise it for the concrete problems you're solving
Finally at this point transformers should become easier and more obvious. Perhaps the first one would be to replace passing the config everywhere with ReaderT. Implementing a few transformers by yourself should help. Start with MaybeT and continue with ReaderT. Tip: `liftIO` magically works but you don't have to always understand how. Maybe also try free monads if transformers seem too annoying!
- atc 10y agoThis is a recommendation I would back for people learning Haskell and don't want to get snagged in the weeds like this. I have been programming in Haskell on and off for a year and eventually got the opportunity to write some REST services in it. It required Servant & PostgreSQL and I didn't generify it too much, just used straight IO. I am now "mature" enough with the language to start generify the API a bit more and depend on things like `MonadIO` instead of direct IO usage, which would also enable better unit testing like those described here: https://lexi-lambda.github.io/blog/2016/10/03/using-types-to-unit-test-in-haskell/ https://lexi-lambda.github.io/blog/2016/10/03/using-types-to... Good luck to GP!
- spion 10y agoI almost forgot; use MVar [1] to keep mutable state around. Its seriously helpful. https://hackage.haskell.org/package/base-4.9.0.0/docs/Control-Concurrent-MVar.html https://hackage.haskell.org/package/base-4.9.0.0/docs/Contro...