Created
February 26, 2019 17:48
-
-
Save boj/c71e49e941ca18d87a82b04afee64b13 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| module Main where | |
| import Control.Monad.Reader | |
| data AppContext = AppContext { a :: String, b :: Int } | |
| newtype AppT a = AppT { runAppT :: ReaderT AppContext IO a } | |
| deriving (Functor, Applicative, Monad, MonadIO, MonadReader AppContext) | |
| -- this function simply runs a side effect | |
| -- in this case printing a value to the console | |
| f :: AppT () | |
| f = do | |
| ctx <- ask | |
| liftIO $ print $ a ctx | |
| g :: Int -> AppT Int | |
| g v = do | |
| _ <- f -- call f for the side effect | |
| ctx <- ask | |
| return (b ctx + v) | |
| main :: IO () | |
| main = do | |
| let ctx = AppContext "string" 10 | |
| flip runReaderT ctx . runAppT$ do | |
| a <- f | |
| liftIO $ print a | |
| b <- g 10 | |
| liftIO $ print b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment