Hemen Ashodia
Hemen Ashodia

Reputation: 509

Scala generating permutations of list runs out of memory

When I tried to generate permutations of a Scala list element using in-build function List(el1,el2..).permutations, heap runs out of memory.

Is there any way to generate this permutations in batches and store them in some file? So that it doesn't run out of memory?

Upvotes: 2

Views: 219

Answers (1)

marstran
marstran

Reputation: 28046

You could do something like this:

list.permutations foreach { x => 
    // Save permutation to file.
}

If you want to save the permutations in bigger chunks, you could group the permutations first (with an appropriate chunk size):

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

The iterator which is returned from the permutations method will throw away permutations after they have been saved to file. It is also lazy, so no other permutations will be computed before the previous chunk was saved.

Upvotes: 4

Related Questions