今天早些时候,我为 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 邮件列表上有关于添加像enumSequence和manyToOne这样的组合器的讨论,但到目前为止,在 enumerator 包中似乎没有任何东西可以提供这种功能。