Home » Java » Functional programming in Java 7 [closed]

Functional programming in Java 7 [closed]

Posted by: admin December 28, 2021 Leave a comment

Questions:

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.

Answers:

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, filter etc. 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#, OCAML and Scala would still be impractical.

Some examples:

  1. The lack of Algebraic Data Types – these are regarded as a key component of the
    statically typed family of FP languages, and go especially well with many FP idioms.
  2. While not exactly a requirement for FP, nearly all statically typed FP languages feature
    some form of type inference.
  3. Statements need to behave like expressions for a lot of Functional Programming
    idioms to be convenient – if, try, etc need to return a value.
  4. Enforcement (as in Haskell), or the encouraging (as in Scala) of
    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