ReaderT Example Simple

ReaderT の簡単な例

:set +m
:set -package mtl

import Control.Monad.IO.Class(liftIO)
import Control.Monad.Trans.Reader
data Config = Config Int Int deriving (Show)
:{
port :: Config -> Int
port (Config x _) = x

client_func :: ReaderT Config IO ()
client_func = do
  p <- asks port
  liftIO $ putStrLn ("number" ++ show(p))
:}
runReaderT client_func (Config 4 10)

出力

number4
import Control.Monad.IO.Class(liftIO)
import Control.Monad.Trans.Reader
data Config = Config Int Int deriving (Show)
:{
port :: Config -> Int
port (Config x _) = x

client_func :: ReaderT Config IO ()
client_func = asks port >>= \p -> liftIO $ putStrLn ("number" ++ show(p))
:}
runReaderT client_func (Config 4 10)

出力

number4