WriterT 相关源码分析
看完ReaderT
的源码,接着来分析WriterT
的源码。
示例
先看一个超级简单的例子:
import Control.Monad
import Control.Monad.Trans.Writer
data LogEntry = LogEntry { msg :: String } deriving (Eq, Show)
calc :: Writer [LogEntry] Integer
calc = do
tell [LogEntry "Start"]
let x = sum [1 .. 10000000]
tell [LogEntry (show x)]
tell [LogEntry "done"]
return x
test = execWriter calc
test2 = runWriter calc
test
执行结果:
[LogEntry {msg = "Start"},LogEntry {msg = "50000005000000"},LogEntry {msg = "done"}]
2021-08-24