質問

I'd like to use either Functional Java or Guava (or less likely Scala) in a course I'll be teaching. Although there are lots of functional languages that run on the JVM I'd like to stick to something that looks as much like Java as possible, i.e., something that will be most compatible, conceptually and syntactically, with the functional features expected in Java 8.

It looks like Functional Java and Guava are the best candidates. I haven't been able to find anything comparing them in terms of capabilities, ease of use, conceptual closeness to straight Java, etc. Does anyone know of a good comparison between these libraries?

役に立ちましたか?

解決

Guava's goal is not to provide functional idioms in Java. From the Functional Explained Guava wiki page:

Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava, and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps.

Please be sure, when using Guava's functional utilities, that the traditional imperative way of doing things isn't more readable. Try writing it out. Was that so bad? Was that more readable than the preposterously awkward functional approach you were about to try?

Leaning too heavily on functional idioms makes not too much sense up to Java 7 as the overhead is too high (see vertical problem). This will change with Java 8 which will change the way Java libraries and programs are design on the detail level. Things that make sense in Java up to 7 will be discouraged to some extent in Java 8. This will motivate a new edition of Effective Java and a lot of new APIs.

If you're trying to teach functional programming it's probably better to stick to a (more or less) pure functional language. Every language that is a melange (or emulation) of FP and OOP will be a distraction.

他のヒント

As stated above, Guava is just a Java library -- a Java 5 compatible library, even (as of release 11). The position of Guava on functional programming is summed up by Kevin Bourrillion:

“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. 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.”

Since Guava is more of a general purpose library that happens to have functional idioms, and Functional Java is purely about implementing functional idioms in Java, Functional Java sounds like a better fit with probably a more complete set of FP-like features.

On the flip side, I prefer Guava because it is more general purpose, and therefore I find myself using several of the features that are unrelated to functional idioms.

One of the problems with both libraries (as noted on Guava's Wiki in the above posts) is the "vertical noise" of anonymous inner classes for your functional objects. Another library that attempts to fix this via annotations and the APT is Jedi.

Another approach that makes any of the above libraries (Guava, FJ, or Jedi) less-noisy without annotations is a library that I wrote, Funcito, inspired by the syntax of Mockito. It is more limited in what it can simplify at this point, essentially wrapping single method calls, but that's what I currently find myself doing most of the time already.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top