• Post Reply Bookmark Topic Watch Topic
  • New Topic

java 8 and FP

 
Randall Twede
Ranch Hand
Posts: 4519
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how much does java 8 support functional programming? I am learning python/jython and they say it fully supports bot FP and imperative styles. in fact they call it an OO language. jython lets you use anything in the java API. how does java 8 compare? any links to a tutorial about using java 8 to do FP would be cool.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an article about your question: How Functional is Java 8? (Sometimes it's a bit technical).

My own opinion: Java 8 has some features that definitely are borrowed from functional languages, and it enables you to use some of the distinct features that functional languages have, but it would go too far to call it "a functional programming language".

If you want to learn real functional programming, then for example Clojure is interesting. It's a dialect of LISP that runs on the JVM. It's totally different from Java.

Haskell is the language that is often regarded as the king of functional programming languages. It's not easy, though.

Last year I followed the Coursera Programming Languages online course. It was very interesting, we played with a number of different programming languages there and learned the fundamentals of different programming language paradigms. I see that it's going to start again soon (25 July), you can join it for free if you're interested.
 
Randall Twede
Ranch Hand
Posts: 4519
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jesper, I will check out that article. that seminar thing sounds interesting too. I don't think I am ready for such a major change as haskel or lisp. I really like what I am learning about jython. to make a GUI I just import awt and swing just like always. but you can also write functional style.
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 96
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java may not be the most functional friendly language, although calling a language "functional" often mainly means that it does not support any other programming paradigm than the functional one. Scala is the exception since it supports imperative programming as well as functional programming. Note that object orientation is not to be opposed to functional programming.

Java misses some constructions that are generally associated with functional programming, although they should not, such as pattern matching and for comprehensions. These constructs are in fact not related to functional programming and could be use in imperative programming as well. If there is one feature that is really missing in Java (regarding functional programming), it is higher kinded types. All other missing features are either functional syntactic sugar or things you can implement yourself, as I show in my book, Functional Programming in Java (Manning).

Note that there is a huge difference between dynamically and statically typed languages which makes it difficult to compare languages like Clojure and Haskell. One may build most of the tools needed for functional programming in Java (including stack safe recursion which, although not specifically related to functional programming, is used (too much?) heavily with this paradigm), but there is no way to use dynamic types with a statically type language (and conversely).

The best things Java 8 brings us to support functional programming are Lambdas.  And methods reference are a very useful addition (not specific to functional programming, by the way).

But if your question was about how much (or how well) the standard Java 8 library supports functional programming, the answer is very poorly. Unfortunately, streams have many problems (from the functional programming point of view), mainly because they are made to work with standard Java lists with in place mutation through effects, instead of using immutable lists and functions. Optional is unusable in many cases because it is not serializable. But it is easy to define immutable lists, functional streams and a correct Option class as well as the numerous other functional constructs that are available in so called "functional" languages
 
Randall Twede
Ranch Hand
Posts: 4519
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Pierre, I am just first learning about functional programming. as I mentioned I am starting to like jython. like scala it is an imperative language(many call it an OO language), but you can use it either way. one big difference from java is there is no typing. but I cut my teeth on languages with no typing ;^)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!