Over a million developers have joined DZone.

Java Forum Nord 2015: 'Functional Programming in Java 8' by Nicole Rauch

At Java Forum Nord 2015, Nicole Rauch gave an overview of the most important aspects of functional programming both in Java and Haskell.

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

This article is part of a series about Java Forum Nord 2015, a conference that took place in Hannover, Germany. Links to articles about other talks I visited there can be found below.

I managed to avoid the topic of functional programming for a long time, which was not good. When functional concepts have been implemented in Java 8 I thought "He cool, now I must not even leave my favorite programming language to learn functional programming". I enjoy using these features and want to learn more about functional programming. This gave me more than enough motivation to visit Nicole's talk.

Nicole began her talk by asking the audience how they would implement an application that sums the squared numbers from 1 to 10 in "good old Java". The solution is the following for-loop:

int sum = 0 ;
for ( int i = 1 ; i <= 10 ; i ++ ) {
sum = sum + i *i ;

Then, she wrote the same code with Java 8 (using streams):

IntUnaryOperator square = x ->; x *x ;
IntBinaryOperator add = (x,y ) -> x +y ;
IntStream. rangeClosed ( 1, 10 ). map (square ). reduce ( 0, add ) ; // 385 

That looks great and way more readable. Then she showed the same in Haskell:

foldl (+) 0 (map (\x ->; x*x) [1..10]) -- 385

There are so few characters I think it cannot be any shorter. Right after that and a few other code examples I thought "I have to learn Haskell". Because I don't want to cite all of her slides here, I just take one of her examples. You can (and should!) have a look at the rest of her slides.

Nicole explained the main concepts of functional programming. A functional language has to include immutability and pure functions and treat functions as first class citizens.

So, what is immutability? An immutable object is an object that doesn't change. In Java, this can be done by never writing set()-methods and also never change the attributes of an object. Instead, objects are created by giving them all attributes in the constructor. Changes are realized by creating whole new objects instead of changing existing ones. This makes these objects thread-safe, because they have a consistent state at any time. Also, they are way easier to construct and test.

A pure function simply is a method without side effects. Its output is only dependent from its input parameters, which is called "referential transparency". If this is true, then this method will give the same result when being called multiple times with the same parameters. Everyone who has debugged a nice, weird legacy codebase knows: These methods are way easier to understand than methods with side effects. Also, they are optimizable by the compiler because the order of execution is arbitrary and results can be cached.

Treating functions as first class citizens means that a function can be referenced and passed around wherever needed.

This makes it possible to use functions as parameters for other functions. These are called higher order functions, because they take other functions as parameters.

Using these concepts results in a number of advantages. There is no complicated modelling of status changes while debugging. Several optimizations can be done automatically. The code is more robust. Effects of code are isolated in one scope and need not be tracked throughout the codebase.

Other content of Java Forum Nord 2015

These are the talks I visited:

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

java ,java 8

Published at DZone with permission of Steven Schwenke. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}