I have written a function which I see great potential in being a recursive lambda function but I just can't wrap my head around how to implement it myself. The reason why I want to convert it to a recursive lambda function is the ability for me to run it for as long as I want where as currently I would have to copy paste another segment of code.
private void crack()
{
System.out.println("Trying 1 mangle");
wordList.forEach(
x -> Mangle.getAllMangles(x).forEach(this::checkAllVictims)
);
System.out.println("Trying 2 mangles");
wordList.forEach(
x -> Mangle.getAllMangles(x).forEach(
y -> Mangle.getAllMangles(y).forEach(this::checkAllVictims)
)
);
System.out.println("Trying 3 mangles");
wordList.forEach(
x -> Mangle.getAllMangles(x).forEach(
y -> Mangle.getAllMangles(y).forEach(
z -> Mangle.getAllMangles(z).forEach(this::checkAllVictims)
)
)
);
}
How would one go about refactoring this function the best way possible?
From a course on functional programming with Elixir a year ago I did something similar but I do not know how to apply the same principle here.
def to_church(0) do
fn(_), y -> y end
end
def to_church(n) do
fn(f, x) -> f.(to_church(n - 1).(f, x)) end
end
The whole Elixir code can be found here if more context is needed.
Another issue I have had, which is not the main issue of this question but would be a cherry on top to have answered, is that when running the above code in parallel as part of a Runnable object in an ExecutorService thread pool (run() calls crack()) all the threads except the first one allocated stops executing after printing "Trying 1 mangle". They simply just dissapear without a trace and I do not know why.