show-prettyprint
Memo
prettyprinter パッケージの Doc 形式にすることができる
ただの文字列も
prettifyShow
関数で整形できるShow のインスタンスであれば何もしなくても勝手に整形するので、結構便利かも?
改行ポイントまで文字数がいかないと何もしない点が良く無い
使い方
ネストした構造
import Data.Map (fromList)
data Record a b = Record { r1 :: a, r2 :: b } deriving Show
data Pair a b = Pair a b deriving Show
nestedExample = fromList
[ ("hello", Left (Pair True ()))
, ("world", Right (Record { r1 = ('c', -1.2e34), r2 = 123 }))
, ("!" , Left (Pair False ()))
]
λ stack repl --package show-prettyprint --package containers
> :l Main.hs
> import Text.Show.Prettyprint
> nestedExample
fromList [("!",Left (Pair False ())),("hello",Left (Pair True ())),("world",Right (Record {r1 = ('c',-1.2e34), r2 = 123}))]
> prettyPrint nestedExample
fromList [("!",Left (Pair False ()))
,("hello",Left (Pair True ()))
,("world",Right (Record {r1 = ('c',-1.2e34),r2 = 123}))]
ただの文字列
import Text.Show.Prettyprint
stringExample = putStrLn . prettifyShow . unlines $
[ "Hello Foo (\"(Bar\", Haha) (Baz (A { foo = C, bar = D, qux = (E,\"He)llo World!\",G,"
, " H,[A,B,c,d,e,Fghi]) } ) (B,C) [Baz A1 B2, (Baz A3 (B4)), (Baz A5 (B6)), (Baz"
, " (A7) B8)]) (Foo) (Bar) (Baz (A) (B))"
]
λ stack repl --package show-prettyprint
> :l Main.hs
*Main> stringExample
Hello Foo ("(Bar",Haha)
(Baz (A {foo = C
,bar = D
,qux = (E,"He)llo World!",G,H,[A,B,c,d,e,Fghi])})
(B,C)
[Baz A1 B2,(Baz A3 (B4)),(Baz A5 (B6)),(Baz (A7) B8)])
(Foo)
(Bar)
(Baz (A) (B))
木構造
import Text.Show.Prettyprint
data Tree a
= Leaf a
| Fork (Tree a) (Tree a)
deriving Show
t1, t2, t3, t4 :: Tree Int
t5 = Fork t4 t4
t4 = Fork t3 t3
t3 = Fork t2 t2
t2 = Fork t1 t1
t1 = Fork t0 t0
t0 = Fork (Leaf 1) (Leaf 2)
λ stack repl --package show-prettyprint
> :l Main.hs
> prettyPrint t0
Fork (Leaf 1) (Leaf 2)
> prettyPrint t1
Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))
> prettyPrint t2
Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
> prettyPrint t3
Fork (Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
(Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
> prettyPrint t4
Fork (Fork (Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
(Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))))
(Fork (Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
(Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))))
> prettyPrint t5
Fork (Fork (Fork (Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
(Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2))
(Fork (Leaf 1) (Leaf 2)))))
(Fork (Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
(Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2))
(Fork (Leaf 1) (Leaf 2))))))
(Fork (Fork (Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
(Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2))
(Fork (Leaf 1) (Leaf 2)))))
(Fork (Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2))))
(Fork (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 1) (Leaf 2)))
(Fork (Fork (Leaf 1) (Leaf 2))
(Fork (Leaf 1) (Leaf 2))))))
リソース
Last updated