Iましhereから取られた以下のコード、:私は)data Blog a
でreaderT
の正確な役割をSQLiteでReaderTとrunReaderTを使用していますか?
1を理解しようとしている
type Blog a = ReaderT SQLiteHandle IO a
data BlogDBException = BlogDBException String deriving (Show, Typeable)
instance Exception BlogDBException
run :: Blog a -> IO a
run m = do
db <- openConnection "myblog.db"
runReaderT m db --runReaderT :: ReaderT r m a -> (r -> m a)
sql :: String -> Blog (Either String[[Row Value]])
sql query = do
db <- ask --ask :: Monad m => ReaderT r m r
liftIO $ do
putStrLn query
execStatement db query
dbQuery :: Blog [Int]
dbQuery = do
r <- sql "select UID from UIDS;"
case r of
Right [rows] -> return [fromIntegral uid | [(_, Int uid)] <- rows]
Left s -> liftIO $ throwIO (BlogDBException s)
_ -> liftIO $ throwIO (BlogDBException "Invalid result")
?
2)ここではrunReaderT
とは何ですか?
3)ask
の機能はどのように機能しますか?
誰でも簡単な説明がありますか?これは初めてReader
モナドで作業しています。