scalaz流io中的append方法导致无限循环

scalaz流io中的append方法导致无限循环,scala,scalaz7,scalaz-stream,Scala,Scalaz7,Scalaz Stream,我在scalaz stream网站上使用了这段代码,它正在使用to方法,但当我尝试使用append时失败了,它看起来像是进入了一个无限循环,永远不会结束。我想使用append方法的原因是我不想用to方法重写文件 io.linesR(t) .intersperse("\n") .pipe(text.utf8Encode) .to(io.fileChunkW(target)) .run.run //success files.foreach(t => { io.l

我在scalaz stream网站上使用了这段代码,它正在使用to方法,但当我尝试使用append时失败了,它看起来像是进入了一个无限循环,永远不会结束。我想使用append方法的原因是我不想用to方法重写文件

io.linesR(t)
  .intersperse("\n")
  .pipe(text.utf8Encode)
  .to(io.fileChunkW(target))
  .run.run //success

files.foreach(t => {
      io.linesR(t)
        .intersperse("\n")
        .pipe(text.utf8Encode)
        .append(io.fileChunkW(target))
        .run.run
    }) //the program keeps running, it looks like in an infinite loop
我很困惑,有人能解释一下这里发生了什么吗


提前多谢

“附加”不是附加到文件,它是一个将一个进程附加到另一个进程的组合器。在你的例子中,我说不出append到底得到了什么,奇怪的是,我认为你得到了无限的函数流ByteVector=>Task[Unit],这就是为什么它永远不会完成

您需要自定义fileChunkW方法,例如,您可以这样做:

def appendFileChunkW(f: String, bufferSize: Int = 4096, append: Boolean = true): Sink[Task,ByteVector] =
    io.chunkW(new BufferedOutputStream(new FileOutputStream(f, append), bufferSize))

files.foreach(t => {
      io.linesR(t)
        .intersperse("\n")
        .pipe(text.utf8Encode)
        .to(appendFileChunkW(target))
        .run.run
    })

我认为如果scalaz stream中有类似的东西就好了。一个友好的建议-在提问之前,请尝试阅读API文档。文档中说,
append
是一个通用函数,它运行一个又一个进程,与文件无关。似乎您可能认为,
append
完全是因为名称恰好与您尝试执行的操作相匹配而执行了其他操作。:)当然,如果文件不清楚,那么一定要问清楚!