Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional programming in Java 7 [closed]

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.

like image 511
Deadlock Avatar asked Nov 29 '12 02:11

Deadlock


People also ask

Why Java is not a functional programming?

Java is not a functional language, it is fundamentally an Object Oriented language that allows us to adopt some functional concepts in so far as we enforce their correct implementation through developer discipline. Unlike in Haskell, Idris, Ocaml (and even Scala) the compiler alone won't save us.

Does Java allow functional programming?

The next big thing what java has been added is that Java has started supporting the functional style of programming with its Java 8 release. In this article, we will discuss functional programming in Java 8. What is functional programming? It is a declarative style of programming rather than imperative.

Does Java 8 streams support functional programming?

However, Java 8 provides us many functional interfaces by default for different use cases under the package java. util. function. Many of these functional interfaces provide support for function composition in terms of default and static methods.

How do you write a closure in Java?

A closure is a function (or method) that refers to free variables in their lexical context. The function is a block of code with parameters. It may produce a result value (return type). A free variable is an identifier used but not defined by the closure.


1 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

like image 125
Faiz Avatar answered Oct 19 '22 20:10

Faiz