【问题标题】:How to merge adjacent lines with scalaz-stream without losing the splitting line如何在不丢失分割线的情况下将相邻线与scalaz-stream合并
【发布时间】:2015-05-05 12:34:44
【问题描述】:

假设我的输入文件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

【问题讨论】:

标签: scala scalaz-stream


【解决方案1】:

以下工作正常,但如果我在不止一个玩具示例上运行它会非常慢(处理 70MB 大约需要 5 分钟)。那是因为我在到处创建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
  }

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 2012-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2019-03-30
    • 1970-01-01
    相关资源
    最近更新 更多