Wednesday 29 June 2016

Functional Programming with Java 8 and javaslang

Previous decade there was a meme about Object oriented programming and now it is, the Functional programming. What it is really? 

Functional programming leverages idea of providing behaviour using functions. Since its inception, Java has always been too verbose. It may be good for birth of a programmer, while he is on embarkation of logical thinking but it takes too many lines of code to solve a problem.  

Ironically, this has added complexity with Java. But with Java 8 the compilers have become incredibly smarter and can infer certain things letting Java coder focus on problem statement. When I first heard Java introducing lamdas, I was so excited to try it out, and finally felt that community is listening and bridging gap between Scala, its other counterpart running on JVM.

Java 8’s lambdas (λ) empower us to create wonderful API’s. They incredibly increase the expressiveness of the language.

Lets try to understand this with an example. We will look at a program that has list of int Strings and then we convert them into list of Integer objects. Later we add an additional integer element to list. And finally we need to print out the even numbers that are greater than 5 among them. See how verbose legacy Java code could be.













































When to go Functional?


Side effects


Real world program will usually have side-effect, meaning they modify state of an object. Functional programming aims towards immutability. This avoids state change with mutable nature of program such as changing objects themselves, or manipulating data and persisting it to database. But in certain cases mutability has its place, and functional style cannot comply to requirement, like objects shared among services and are suppose to be mutable. Immutability provides thread safety, reliable hash, type-safe for unchecked casting and does not require cloning.
















Referential transparency


Functions should purely depend on input. If they depend on state then that violates reusability. Functional style acts like shared services that act upon set of input parameters and are more consistent in nature. A function, or more general an expression, is called referential transparent if a call can be replaced by its value without affecting the behaviour of the program.














With plusOne function, the compiler can easily determine that given an input of 5, the expression plusOne(5) will evaluate to 6.  It can just replace that with 6 and make it faster at runtime.

In second example, let's say "G" can be modified externally, by something outside of the compiler's control. It can't replace plusG(5) with anything.  The expression can only be evaluated at runtime because the potential of modification makes it referentially “opaque".

Decoupled


Functions can go along with applications that can be defined as decoupled components. Each function having single responsibility and must be generic.

Exploring Javaslang 

http://www.javaslang.io

Java 8 gives us opportunity to write precise and decoupled code and opened door for other libraries to provide more that it couldn't simply offer out of its hat. Functional programming is all about values and transformation of values using functions. Javaslang functional interfaces are Java 8 functional interfaces on steroids. They also provide features like:

  • Composition
  • Lifting
  • Currying
  • Memoization


Composition 


The term "composition of functions" refers to the combining of functions in a manner where the output from one function becomes the input for the next function creating third function. You could use andThen instead.










More, Coming soon...