WriterT is a monad transformer for accumulating output while an underlying monad performs other effectful computations. Its type, WriterT w m a, identifies the collected output w, underlying monad m, and computation result a. section 15.1.2
Use WriterT through a small set of practical choices:
- Choose an output type with a Monoid instance so independently produced values can be combined. section 15.1.2
- Call tell to add output without replacing the computation's ordinary result. section 15.1.2
Collecting directories during synchronization
A file-processing application can use type App a = WriterT [FilePath] IO a. After listing a directory, tell dirs records its subdirectories; recursive processing continues normally. Running runWriterT (example "foo") produces ((),["baz","bar"]).
For richer reports, define structured entries such as Success, Failure, and Skipped, wrap them in newtype Log = Log [LogEntry], and derive Semigroup and Monoid for the wrapper.
