是否有一种很好的方法将此Java功能转换为递归,可扩展的[lambda]函数
我写了一个功能,我认为这是递归lambda功能的巨大潜力,但我只是无法围绕如何自己实施它来缠绕。我要将其转换为递归lambda函数的原因是,只要我想要,我就可以将其运行,而我必须像目前必须复制粘贴的另一个代码。
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)
)
)
);
}
最好的方法如何重构此功能?
从一年前与Elixir的功能编程课程中,我做了类似的事情,但我不知道如何在此处应用相同的原则。
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
可以找到整个长生不老药如果需要更多上下文。
我遇到的另一个问题,不是这个问题的主要问题,而是最重要的樱桃,是在同时运行上述代码作为executorService线程池中可运行对象的一部分时(run(run())调用crack())除了第一个分配的停止在打印“尝试1 mangle”之后执行的所有线程。他们只是在没有痕迹的情况下消失了,我不知道为什么。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了一些解决方案。
我创建了一个新的类,该类实现了Biconsumer< list; integer>。
然后,我可以在cracker类的crack()函数中调用它:
我还更改了并行性,仅适用于饼干的字典,而不是使整个类都可以运行。
I came up with somewhat of a solution.
I created a new class which implemented BiConsumer<List, Integer>.
Which I could then call in the crack() function of my Cracker class:
I also changed the parallelism to apply only to the dictionary of the Cracker instead of making the whole class a Runnable.
参见使用Java 8 实现递归lambda函数
以及
and https://dzone.com/articles/do-it-it-it-it-it-it-it-it-it-it-it-iit-it-java -8恢复lambdas
和 https://coderanch.com/ttps:/ -java 。
似乎您应该至少找到一个可行的解决方案。
See Implement recursive lambda function using Java 8
as well as https://pysaumont.github.io/2014/09/01/Recursive-lambdas-in-Java-8.html
and https://dzone.com/articles/do-it-java-8-recursive-lambdas
and https://coderanch.com/t/634917/java/Recursion-Lambda-Java.
Seems that you should find at least one viable solution.