简体   繁体   English

Guava中的任何东西都类似于Functional Java的效果?

[英]Anything in Guava similar to Functional Java's Effect?

I know one of the goals of pure functional programming is to eliminate mutability, and therefore to preclude side-effects. 我知道纯函数式编程的目标之一是消除可变性,从而排除副作用。 But let's face it, Java is not a functional language even with all of the functional-programming libraries that exist. 但是让我们面对现实吧,即使存在所有的函数式编程库,Java也不是一种函数式语言。 In fact it seems that some of the FP-libraries know and expect this. 事实上,似乎有些FP库知道并期待这一点。 For instance in Functional Java, there is the Effect class. 例如,在Functional Java中,有Effect类。 In the Jedi FP library, there is the Command interface. 在Jedi FP库中,有Command接口。 This allows you to -- among other things -- apply a command pattern with type-safety to elements of an Iterable without the nasty for-loop boilerplate. 这允许您 - 除其他外 - 将类型安全的命令模式应用于Iterable元素,而不需要令人讨厌的for循环样板。

Command<PhoneNumber> makeCall = new Command<PhoneNumber> {
    public void execute(PhoneNumber p) { p.call(); }
}
List<PhoneNumber> phoneList = ...
FunctionalPrimitives.forEach( phoneList, makeCall );

So the question is, is there anything like that in Guava? 所以问题是,番石榴中有类似的东西吗?

EDITED AFTER ANSWER ACCEPTED FOR CLARIFICATION 在接受澄清后接受回复

I am developing a framework that helps with the "vertical problem" inherent in most Java FP-libraries, under a certain set of circumstances. 我正在开发一个框架 ,它可以帮助解决大多数Java FP库中固有的“垂直问题”,在某些情况下。 So I would not actually make the code example as shown above: ie, explicitly declare a new class implementation of Command with all of its vertical-noise icky-ness, simply for the purpose of immediately applying it right after the declaration. 所以,我不会实际上使如上图所示的代码示例:即明确宣布一个新的类实现的Command其所有的垂直噪声过甜的烦躁,只需在声明之后立即应用它的目的。

I was thinking more along the lines of the actual command pattern, where there may be several possible commands declared elsewhere, and only one of them gets passed into the code which wants to apply it iteratively. 我更多地考虑实际的命令模式,其中可能有几个可能的命令在其他地方声明,并且只有其中一个被传递到想要迭代应用它的代码中。 Furthermore, the goal of my framework is to make it more idiomatic to create functional-interface objects (functions, predicates, commands, other simple lambdas) without simply moving the vertical problem elsewhere. 此外,我的框架的目标是使创建功能接口对象(函数,谓词,命令,其他简单的lambda)更加惯用,而不是简单地将垂直问题移到别处。 I have long realized this is not within the scope of Guava. 我早就意识到这不在番石榴的范围内。 But as Command-like interface are available in other FP libraries, I just wanted to know if an analog existed in Guava. 但是由于其他FP库中有类似Command的接口,我只想知道Guava中是否存在模拟。

A more complete code example, using my framework, might be something like this: 使用我的框架的更完整的代码示例可能是这样的:

class Stuff {
    private final Stuff CALLS_TO = callsTo(Stuff.class); // a proxy
    public static final Command<Stuff> CMD1 = commandFor(CALLS_TO.someMethod1());
    public static final Command<Stuff> CMD2 = commandFor(CALLS_TO.someMethod2());

    // methods exist for use elsewhere, but are conveniently also wrapped as commands
    public void someMethod1() {...}
    public void someMethod2() {...}
}

class Activity {
    public void handleIt(List<Stuff> stuffs, Command<Stuff> doCmd) {
        doSomeThings();
        ...
        forEach(stuffs, doCmd);
        ...
        doOtherThings();
    }
}

Nope! 不!

Kevin Bourrillion, the Guava project lead, has said on Guava's functional features: Guava项目负责人Kevin Bourrillion对Guava的功能特性说:

“The syntax sucks. “语法很糟糕。 At the same time, this stuff is now, has always been and will always be nothing but a stopgap measure until the right language change can come along, at which time we can finally really decide on the optimal syntax and have functional-style programming start actually making lives better in Java for once. 与此同时,这些东西现在,一直都是,并且将永远只是一个权宜之计,直到正确的语言变化出现,此时我们终于可以真正决定最佳语法并开始功能式编程实际上,在Java中让生活变得更好。 So I'm undecided how much effort to put into the Function/Predicate stuff; 所以我决定投入功能/谓词的工作量是多少; it's in the library more because it sort of had to be, not so much because we think it's a crown jewel.” 它更像是在图书馆,因为它必须是,而不是因为我们认为它是一个皇冠上的宝石。“

We will probably change our strategy significantly when Java 8 comes along, but that won't be for a while yet. 当Java 8出现时,我们可能会大大改变我们的策略,但这还不会有一段时间。

Also, we haven't found many use cases for which we think the Command interface you describe would be the best solution. 此外,我们还没有发现很多用例,我们认为您描述的Command接口是最佳解决方案。 For example, we think that your above code would be much better written as 例如,我们认为您的上述代码可以更好地编写为

for(PhoneNumber phone : phoneList) {
  phone.call();
}

the old-fashioned way. 老式的方式。 We could potentially be convinced of the merit of Command , but I think the "for-each" use case is almost always better done the old-fashioned way. 我们可能会相信Command的优点,但我认为“for-each”用例几乎总是以老式的方式做得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM