6

今天早些时候,我为 iteratees 编写了一个小型测试应用程序,它组成了一个 iteratee,用于编写进度,一个 iteratee 用于实际复制数据。我最终得到了这样的价值观:

-- NOTE: this snippet is with iteratees-0.8.5.0
-- side effect: display progress on stdout
displayProgress :: Iteratee ByteString IO ()

-- side effect: copy the bytestrings of Iteratee to Handle
fileSink :: Handle -> Iteratee ByteString IO ()

writeAndDisplayProgress :: Handle -> Iteratee ByteString IO ()
writeAndDisplayProgress handle = sequence_ [fileSink handle, displayProgress]

在查看枚举器库时,我没有看到sequence_or的类似物enumWith。我想做的就是组合两个迭代器,以便它们作为一个迭代器。我可以丢弃结果(()无论如何都会如此)或保留它,我不在乎。(&&&) 来自 Control.Arrow 是我想要的,仅适用于迭代而不是箭头。

我尝试了这两个选项:

-- NOTE: this snippet is with enumerator-0.4.10
run_ $ enumFile source $$ sequence_ [iterHandle handle, displayProgress]
run_ $ enumFile source $$ sequence_ [displayProgress, iterHandle handle]

第一个复制文件,但不显示进度;第二个显示进度,但不复制文件,因此显然内置 sequence_ 对枚举器的迭代器的影响是运行第一个迭代器直到它终止然后运行另一个,这不是我想要的。我想并行而不是串行运行迭代器。我觉得我遗漏了一些明显的东西,但是在阅读wc枚举器库的示例时,我看到了这个奇怪的评论:

-- Exactly matching wc's output is too annoying, so this example
-- will just print one line per file, and support counting at most
-- one statistic per run

我想知道这句话是否表明在枚举框架内组合或组合迭代是不可能的。普遍接受的正确方法是什么?

编辑

似乎没有内置的方法可以做到这一点。Haskell 邮件列表上有关于添加像enumSequencemanyToOne这样的组合器的讨论,但到目前为止,在 enumerator 包中似乎没有任何东西可以提供这种功能。

4

1 回答 1

2

在我看来,与其试图让两个Iteratees并行消耗序列,不如通过一个Enumeratee简单地计算通过它的字节数的身份来提供流。

这是一个简单的示例,它复制文件并打印每个块之后复制的字节数。

import System.Environment
import System.IO
import Data.Enumerator
import Data.Enumerator.Binary (enumFile, iterHandle)
import Data.Enumerator.List (mapAccumM)
import qualified Data.ByteString as B

printBytes :: Enumeratee B.ByteString B.ByteString IO ()
printBytes = flip mapAccumM 0 $ \total bytes -> do
    let total' = total + B.length bytes
    print total'
    return (total', bytes)

copyFile s t = withBinaryFile t WriteMode $ \h -> do
    run_ $ (enumFile s $= printBytes) $$ iterHandle h

main = do
    [source, target] <- getArgs
    copyFile source target
于 2011-07-17T20:20:15.520 回答