admin管理员组

文章数量:1305009

I have some data structure which are several levels deep and produce extremely long and skinny pretty printed output. It would be desirable, if only the first level (generally n levels) would prettyprint and the rest shown in the regular compact format.

I looked at the various pretty-printer on hackage, but could not find one with a functionality to pretty-print structured records up to a specified depth. Is there such a tool or how can it be constructed?

I have some data structure which are several levels deep and produce extremely long and skinny pretty printed output. It would be desirable, if only the first level (generally n levels) would prettyprint and the rest shown in the regular compact format.

I looked at the various pretty-printer on hackage, but could not find one with a functionality to pretty-print structured records up to a specified depth. Is there such a tool or how can it be constructed?

Share Improve this question asked Feb 3 at 21:56 user855443user855443 2,9485 gold badges32 silver badges45 bronze badges 2
  • I recall an approach named "scrap your boilerplate" that could (I guess?) in principle be used to this task. It's not straightforward to learn though. I wonder if there are more modern simpler alternatives nowadays. – chi Commented Feb 3 at 23:27
  • You can always hand-roll your show instance? (recursively passing down the nesting level and decreasing it by one each time) – mb21 Commented Feb 4 at 10:33
Add a comment  | 

1 Answer 1

Reset to default 0

One possible line of attack: look at prettyprinter. It renders documents to an intermediate form called SimpleDocStream. So you might try writing a renderer which either counts the stack depth or limits the indent width. Not exactly what you asked for, but something like that sounds like it would solve your problem.

本文标签: haskellPretty print only top n levels of a data structureStack Overflow