I know that there are a lot of related questions regarding java.lang.OutOfMemoryError: GC overhead limit exceeded (for example How to avoid java.lang.OutOfMemoryError?) and I know that my issue is caused by my bad implementation but I don't know how to fix it.
So, my goal is to get all possible combinations with repetitions from a List. I achieved this task by using the code proposed in the answer by Mark Lister of this question. It works perfectly, but apparently getting all combinations within a range of 10 by using a List with 52 Elements is too much for my computer. Any ideas how I can fix this?
Probably better than just gathering huge Lists of combinations, I could just process each word when producing the combo?
Here is my code:
object MyApp extends App {
val letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toList
run(10)
def mycomb[T](n: Int, l: List[T]): List[List[T]] =
n match {
case 0 => List(List())
case _ => for (el <- l;
sl <- mycomb(n - 1, l dropWhile {
_ != el
}))
yield el :: sl
}
def comb[T](n: Int, l: List[T]): List[List[T]] = mycomb(n, l.distinct)
def run(n: Int): Unit = {
for (i <- 1 to n) {
val combo = comb(i, letters)
combo.par.foreach { word =>
println(word.mkString(""))
// Process each word one by one in parallel
}
}
}
}
.par.