Skip to content

Instantly share code, notes, and snippets.

@boj
Created February 26, 2019 17:48
Show Gist options
  • Select an option

  • Save boj/c71e49e941ca18d87a82b04afee64b13 to your computer and use it in GitHub Desktop.

Select an option

Save boj/c71e49e941ca18d87a82b04afee64b13 to your computer and use it in GitHub Desktop.
{-# 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