Created
April 30, 2019 01:12
-
-
Save huseyinyilmaz/3de624a38a2379f3a00ff43edd6190fb to your computer and use it in GitHub Desktop.
example monad transformer stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | An example module. | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Example (main) where | |
import Control.Monad.Reader(ReaderT, runReaderT) | |
import Control.Monad.Reader(MonadReader) | |
import Control.Monad.Reader(MonadIO, liftIO) | |
import Control.Monad.Reader(ask) | |
import Control.Lens | |
newtype User = User { userName :: String } deriving(Show) | |
newtype AppConfig = AppConfig { | |
_appConfigUser:: User | |
} deriving (Show) | |
class HasAppConfig a where | |
getAppConfig :: Lens' a AppConfig | |
getUser :: Lens' a User | |
getUser = getAppConfig . getUser | |
instance HasAppConfig AppConfig where | |
getAppConfig = id | |
getUser = lens _appConfigUser (\config user -> config{_appConfigUser=user}) | |
newtype App a = App | |
{ | |
unApp:: (ReaderT AppConfig IO) a | |
} deriving | |
( | |
Functor, | |
Applicative, | |
Monad, | |
MonadReader AppConfig, | |
MonadIO | |
) | |
run :: App a -> AppConfig -> IO a | |
run a config = runReaderT (unApp a) config | |
getUserApp :: ( | |
MonadReader r m, | |
HasAppConfig r, | |
Monad m | |
) => m String | |
getUserApp = do | |
env <- ask | |
let user = env ^. getUser :: User | |
return (userName user) | |
-- | An example function. | |
app :: App () | |
app = do | |
name <- getUserApp | |
liftIO $ putStrLn name | |
main :: IO () | |
main = do | |
run app appConfig | |
where | |
appConfig = (AppConfig (User "huseyin")):: AppConfig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment