如何在不丢失分割线的情况下将相邻行与scalaz-stream合并

时间:2015-05-05 12:34:44

标签: scala scalaz-stream

假设我的输入文件myInput.txt如下所示:

~~~ text1
bla bla
some more text
~~~ text2
lorem ipsum
~~~ othertext
the wikipedia
entry is not
up to date

也就是说,有~~~分隔的文档。所需的输出如下:

text1: bla bla some more text
text2: lorem ipsum 
othertext: the wikipedia entry is not up to date

我该怎么做?以下看起来很不自然,加上我失去了标题:

 val converter: Task[Unit] =
    io.linesR("myInput.txt")
      .split(line => line.startsWith("~~~"))
      .intersperse(Vector("\nNew document: "))
      .map(vec => vec.mkString(" "))
      .pipe(text.utf8Encode)
      .to(io.fileChunkW("flawedOutput.txt"))
      .run

  converter.run

1 个答案:

答案 0 :(得分:0)

以下工作正常,但如果我在一个玩具示例上运行它(约5分钟处理70MB),它会非常慢。是因为我正在创建Process的所有地方吗?此外,它似乎只使用一个核心。

  val converter2: Task[Unit] = {
    val docSep = "~~~"
    io.linesR("myInput.txt")
      .flatMap(line => { val words = line.split(" ");
          if (words.length==0 || words(0)!=docSep) Process(line)
          else Process(docSep, words.tail.mkString(" ")) })
      .split(_ == docSep)
      .filter(_ != Vector())
      .map(lines => lines.head + ": " + lines.tail.mkString(" "))
      .intersperse("\n")
      .pipe(text.utf8Encode)
      .to(io.fileChunkW("correctButSlowOutput.txt"))
      .run
  }