没有 space 与 wl-pprint 中的空文档连接

No space concatenating with an empty doc in wl-pprint

我正在使用 wl-pprint to output a data type with its fields rendered and separated with spaces. Some of the fields can be Nothing, so I render them as empty. Obviously, I don't want to add an extra space next to an empty string. It was easily achievable with HughesPJ library,因为它的 <+> 运算符以 empty 为单位。但是在 wl-pprint 中 <+> 中的 space 不是可选的。

我自己很容易实现,给定一个函数来测试 Doc 是否为空。我的另一个问题是没有人需要检查空 Doc 吗?会不会是我算法设计的缺陷?

这回答了 "test if Doc is empty" 部分。

renderCompact 给你一个 SimpleDoc,你可以对 SEmpty 进行模式匹配。

看来够懒了,例如

case ( renderCompact $ vcat $ repeat $ text "foobar" ) of 
   SEmpty -> True ; _ -> False