![](/img/trans.png)
[英]Haskell-pipes: how to use drawAll to test a producer with a MonadSafe constraint?
[英]Haskell Pipes: How do I sort the output of a producer?
我有以下代碼:
import Control.Monad (unless)
import Pipes
import qualified Pipes.Prelude as P
import System.FilePath.Posix ((</>))
import System.Posix.Directory (DirStream, openDirStream, readDirStream)
produceFiles :: DirStream -> Producer FilePath IO ()
produceFiles ds = do
path <- lift $ readDirStream ds
yield path
unless (path == "") $ produceFiles ds
getDC :: FilePath -> Producer FilePath IO ()
getDC top = do
ds <- lift $ openDirStream top
produceFiles ds
runTest top = runEffect $ getDC top >-> P.map (top</>) >-> P.stdoutLn
它將打印目錄top
中的所有文件。 在打印之前如何對輸出進行排序? 我是否需要編寫一個使用者,該使用者首先將輸出“拉”到一個列表中,然后對其進行排序? 我正在使用管道4.1.4。
toListM
的Pipes.Prelude
將生產者轉換為列表。 我們可以使用它,然后再不用pipes
:
runTest top = do
ds <- P.toListM (getDC top >-> P.map (top</>))
mapM_ print $ sort ds
或使用通常的單子運算符更像管道:
runTest top = P.toListM (getDC top >-> P.map (top</>)) >>= mapM_ print . sort
抓住所有Producer
內容將我們帶到了流抽象之外,這就是為什么toListM
返回純列表而不是管道的原因。
是的,您需要首先將輸出消耗掉,或者排入其他結構的列表中。 排序本質上是非流式的,因為例如它可能是最后一個進入的元素應該是第一個出現的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.