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

Resuming the permutation generation in Scala after using grouped permutation generation

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

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

您需要保存用于跟踪流程进展情况的中间数据结构。你不能用内置函数做到这一点,但你可以查看源代码来编写你自己的排列生成器,一旦你弄清楚如何提取状态(然后在你想恢复时再次插入它)。

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

您可以使用 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
}