wl-pprint-extras中的可选空间和多个备选项 [英] Optional spaces and multiple alternatives in wl-pprint-extras
问题描述
是否有一种很好的/可接受的方式来获得两种以上的可选布局,特别是支持可选空间的方式?
特别是,我可能想要打印以下列三种方式之一列出:
[1,2,3,4,5]
[1,2,3,4,5]
[1
,2
,3
,4
,5
]
按优先顺序排列。
你可以在 group
和 flatAlt
/ p>
(< |>):: Doc e - >文件e - > Doc e
a< |> b = group $ flatAlt b a
infixl 5< |>
我们将定义您的三个示例。
{ - #LANGUAGE OverloadedStrings# - }
import Text.PrettyPrint.Free
import System.IO
items = map char ['1'..'5']
最宽=括号。 hcat。标点符号,$ items
wide =括号。 hcat。标点符号,$ items
skinny =括号。 hcat。点缀,。 map(\ x - ><> x> linebreak)$ items
这些适合在屏幕上的宽度最大的是 widestFits
,可以用< |>
widestFits = widest< |>宽< |>我们将尝试在三个文档上打印这些文档,其中一个文档足够宽以容纳<$ c
$ b
c> widest 例子,只有一个宽度足以容纳 wide
示例,另一个不能容纳。
main = do
displayIO stdout $ renderPretty 1.0 15 widestFits
putStrLn
displayIO stdout $ renderPretty 1.0 14 widestFits
putStrLn
displayIO stdout $ renderPretty 1.0 10 widestFits
putStrLn
[1,2,3,4,5]
[ 1,2,3,4,5]
[1
,2
,3
,4
,5
]
Is there a good/accepted way to get more than two alternative layouts, specifically in a way which supports optional spaces?
In particular, I might want to print a list in one of three ways:
[1, 2, 3, 4, 5]
[1,2,3,4,5]
[ 1
, 2
, 3
, 4
, 5
]
in that order of preference.
You can try one alternatives before another with group
and flatAlt
.
(<|>) :: Doc e -> Doc e -> Doc e
a <|> b = group $ flatAlt b a
infixl 5 <|>
We'll define your three examples.
{-# LANGUAGE OverloadedStrings #-}
import Text.PrettyPrint.Free
import System.IO
items = map char ['1'..'5']
widest = brackets . hcat . punctuate ", " $ items
wide = brackets . hcat . punctuate "," $ items
skinny = brackets . hcat . punctuate "," . map (\x -> " " <> x <> linebreak) $ items
The widest of these that fits on the screen, widestFits
, can be defined in terms of <|>
which we defined above.
widestFits = widest <|> wide <|> skinny
We will try pretty printing these on three documents, one wide enough to hold the widest
example, one only wide enough to hold the wide
example, and one that can't hold either.
main = do
displayIO stdout $ renderPretty 1.0 15 widestFits
putStrLn ""
displayIO stdout $ renderPretty 1.0 14 widestFits
putStrLn ""
displayIO stdout $ renderPretty 1.0 10 widestFits
putStrLn "
This results in the desired output.
[1, 2, 3, 4, 5]
[1,2,3,4,5]
[ 1
, 2
, 3
, 4
, 5
]
这篇关于wl-pprint-extras中的可选空间和多个备选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!