如何编写一个自定义的漂亮打印机
在我的职业生涯中经常出现的一个问题是我有某种数据结构(可能是 s 表达式),并且我想以人类可读的形式打印它,并提供合理的缩进选择。
是否有书籍或博客文章描述了如何优雅地做到这一点? 我对算法比对特定库更感兴趣。
A problem that has frequently come up in my career is I have some kind of data structure (perhaps an s-expression) and I want to print it in a human readable form complete with reasonable indentation choices.
Is there a book or blog entry that describes how to do this elegantly? I am interested in the algorithm more than a specific library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
S-Exp 相当于树结构,如果你可以漂亮地打印一棵树,那么你就可以漂亮地打印一个 s-exp。
例如,比较:
与:
算法是相同的,唯一的区别是要打印的周围数据的数量。
本文描述了一种漂亮打印树的算法
< a href="ftp://reports.stanford.edu/pub/cstr/reports/cs/tr/79/770/CS-TR-79-770.pdf" rel="noreferrer">这个描述了一个用于编程语言的漂亮打印机
S-Exps are equivalent to tree structures, if you can pretty-print a tree you can pretty-print an s-exp.
For instance, compare:
to:
The algorithm is identical, the only difference is the ammount of surrounding data you want to print out.
This paper describes an algorithm for pretty-printing trees
This one describes a pretty-printer for programming languages