Does Java 7 have functional programming support or I still need to use FunctionalJava or another lib? I thought is has support to this, but didn’t find much info about it.
Does Java 7 have functional programming support or I still need to use FunctionalJava
Share
It would depend on your definition of Functional Programming.
In any case, the answer to your question would be No. Lambdas were due to appear in Java7 at one point but they will appear only in Java8. It looks like with Java8 you’ll be able to do a lot with the new lambda notation in conjunction with the regular JDK8 class libraries (collections in particular) before you need something like FunctionalJava, but that sort of depends on how much you want to do. A lot of OO folk will be very happy with just a flavor of FP – a common example is collections with
map,filteretc. That by itself would no doubt move Java closer to FP – and might just be FP enough for you.The question is, even then, would that allow true (even if ‘impure’) functional programming in Java? Yes, and No. Yes, because any language with lexical closures and a lambda notation could in theory be enough. No, because FP as supported by languages
Haskell,F#,OCAMLandScalawould still be impractical.Some examples:
statically typed family of FP languages, and go especially well with many FP idioms.
some form of type inference.
idioms to be convenient –
if,try, etc need to return a value.single assignment as well as immutability, and a useful collection
of data structures and libraries to that end.
Other languages like Lisp/Scheme or Erlang are also considered Functional; but in a less strict sense; the requirements (1), and (2) do not apply because they are dynamically typed to begin with.
One can say then, that Javascript is about as functional as Lisp (impure dynamic functional language), because Javascript has always had lambdas and first-class functions. But Java being in the statically typed family, does not fare any better (than Javascript) and certainly not as well as the existing statically typed FP languages.
Regarding (4. (Immutable/Side-effect free)), it appears that in JDK8, the existing mutable classes will be retrofitted with lambda-consuming methods, so that’s something that will (at least for a while) limit how far you can take FP paradigms in Java8.
I found these links very useful – I haven’t been following up for a while though, so I’m not sure if they are the best/latest info regarding this. But worth reading:
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html
http://cr.openjdk.java.net/~briangoetz/lambda/collections-overview.html