Hemen Ashodia
Hemen Ashodia

Reputation: 509

Resuming the permutation generation in Scala after using grouped permutation generation

How to generate permutations of list using grouped method in such a way where we can save the permutations in the file after that if we stop the program it should save the last status and when we re run the program it can resume from where it was last.

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

Upvotes: 0

Views: 39

Answers (2)

bjfletcher
bjfletcher

Reputation: 11518

You can use drop and zipWithIndex to assist with the resumption. drop to skip to the specified group index. zipWithIndex to add indexes to the groups.

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
}

Upvotes: 1

Rex Kerr
Rex Kerr

Reputation: 167901

You'd need to save the intermediate data structures that keep track of how the process is going. You can't do that with the built-in function, but you can look at the source code to write your own permutation generator once you figure out how to extract the state (and then insert it again when you want to resume).

Standard serialization might work but I haven't tested it.

Upvotes: 1

Related Questions