![](/img/trans.png)
[英]haskell pipes - how to repeatedly perform a takeWhile operation on a bytestring pipe?
[英]how to insert an IO operation into a Pipe
我有一個 Haskell function ,它給出了一個目錄,從中遞歸地獲取所有文件並將文件名寫入文件。 這是一個簡單的例子。 在下一步中,我必須通過使用文件內容的操作替換從文件到文本的映射( transf
操作); 這顯然是 IO monad 中的一個操作。
我對Pipe
的了解非常有限; 我嘗試了一個簡單的操作opex
,我試圖將其lift
為 pipe。 我想刪除當前的傳輸
這看起來像一個簡單的問題,但盡管搜索了 web,但我找不到解決方案。 謝謝你的幫助!
pipedDoIO2 :: Path Abs File -> Path Abs Dir -> (Path Abs File -> IO Text) -> ErrIO ()
pipedDoIO2 file path transf = do
hand <- openFile2handle file WriteMode
Pipe.runEffect $
getRecursiveContents path
>-> PipePrelude.map ( transf) -- some IO type left?
-- >-> lift opex
>-> PipePrelude.toHandle hand
closeFile2 hand
return ()
opex :: (Path Abs File -> IO Text)
opex = return . showT
更多閱讀使我得到簡單的答案:使用mapM
中的 mapM。 我希望這個解決方案可以幫助其他人找到這個在 web 上不容易檢測到的“明顯”解決方案; 我在文件擴展名上添加了一個filter
作為如何使用filter
的示例。
警告: toHandle
“使用 hPutStrLn 將字符串寫入句柄”,即它在每次插入后插入一個\n
。
-- a convenient function to go through a directory and
-- recursively apply a function to each file or directory
-- filters for extension md
pipedDoIO2 :: Path Abs File -> Path Abs Dir -> (Path Abs File -> ErrIO String) -> ErrIO ()
pipedDoIO2 file path opex = do
hand <- openFile2handle file WriteMode
Pipe.runEffect $
getRecursiveContents path
>-> PipePrelude.filter (hasExtension (Extension "md"))
>-> PipePrelude.mapM opex
>-> PipePrelude.toHandle hand
closeFile2 hand
return ()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.