使用分组置换生成后,在Scala中恢复排列生成

时间:2015-07-08 19:16:15

标签: scala permutation

如何使用分组方法生成列表的排列,以便我们可以在此之后保存文件中的排列 如果我们停止程序,它应该保存最后的状态和 当我们重新运行程序时,它可以从最后的位置恢复。

list.permutations.grouped(chunkSize) foreach { x =>
    // Save chunk of permutations to file.
}

2 个答案:

答案 0 :(得分:1)

您需要保存中间数据结构,以跟踪流程的进展情况。您不能使用内置函数执行此操作,但是一旦您弄清楚如何提取状态(然后在您想要恢复时再次插入),您可以查看源代码以编写自己的置换生成器。 / p>

标准序列化可能有效,但我还没有测试过。

答案 1 :(得分:1)

您可以使用dropzipWithIndex来协助恢复。 drop跳转到指定的组索引。 zipWithIndex将索引添加到组中。

val start = 0 // get starting position from persistence
list.permutations.grouped(chunkSize).zipWithIndex.drop(start).foreach { case (ps, i) =>
  // persist i, the group index, and ps, the permutations
}