簡體   English   中英

如何在Haskell中收集管道輸出到文件

[英]How to collect output from pipes to file in Haskell

我有一個生產管道,我使用了打印管道。 我想將輸出寫入文件。 如果我使用Control.Data.sinkFile,就像

test2file = runPipe $ CB.sinkFile "testOutput" <+< traverseTree fn3 

我收到類型錯誤:

Couldn't match expected type `Pipe
                                b0 void-0.5.8:Data.Void.Void m0 r0'
            with actual type `Data.Conduit.Internal.Pipe
                                l0 Data.ByteString.Internal.ByteString o0 r1 m1 r1'
In the return type of a call of `sinkFile'
In the first argument of `(<+<)', namely `sinkFile "testOutput"'

如何將sinkFile轉換為可以組合的管道。 有嚴格與懶惰的問題嗎?

解決方案很簡單,編寫一個附加到文件的fileSink函數

fileSink = forever $ do 
inp <- await
liftIO $ appendFile "testOutput" ('\n' : show inp )
return () 

使用文件句柄可能更有效,更實用,將文件名作為參數傳遞。 使用管道真的很容易!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM