![](/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.