Thursday, October 16, 2014

Functional Programming in java


As I intent in the title, will be more focused and descriptive on Functional Programming.
Will have a tiny discussion on how to achieve it in Java as I will have another post on that.

Functional programming(FP) is a resurgent programming paradigm, with initial appearance dates back to 1950s and implementing language was LISP.
So why is it again gaining so much attention. What is the craze. We will see all that.

What is Functional Programming (Hard to Define)-
Functional Programming derives its name from Mathematical Function. And it also works exactly like that.
A mathematical function takes an input or a set of inputs works on them and produces a single or set of out puts. The output depends only on the input nothing else like environment, state of system etc.

In functional programming functions are the primary entity and programs are executed by evaluating expressions. FP requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments
to other functions or be returned as a result of a function.

So to adopt FP you need to design your program as independent small functions.

As Uncle Bob says Functional programming is programming without assignment statements. Is it true ?
Yes it is true. FP is based on Immutability and Stateless. Will see this after knowing the reason of its resurgence.

Cause of its resurgence-

So why OO language like java and other are adhering to it, why they see a future in it?
The reason is concurrency.Let me explain it
Multi-core processors are very common in Today's world. To make the real use of multi-core your application
needs to have multi threaded. When multi threading comes picture it always accompanied by the concurrency issue.
i.e the issue arise when multiple threads accessing the same shared data. And to deal with this we use different
techniques like synchronization, semaphores, volatile variables etc.
And all these are the bottle neck to your application performance.

Simply when one thread is waiting to get access to a data which is being used by another thread, is a wastage of CPU cycles.
Why it is waiting for that thread to release the data, why cant it just access the data. Because the state/value of the data may be
modified by the currently accessing thread, and it will have an awe full consequence known as Race Condition.

Imagine a situation where the sate of the data is unchangeable or Immutable. The threads can execute simultaneously utilizing
the full CPU cycle, producing a better throughput.Functional programming aims at it, immutability.

Features of FP-
I should say the ideology of on which FP depends are-

First, data in functional programs should be immutable, which sounds serious but just means that it should never change.
At first, this might seem odd (after all, who needs a program that never changes anything?), but in practice,
you would simply create new data structures instead of modifying ones that already exist.
For example, if you need to manipulate some data in an array, then you’d make a new array with the updated values, rather than revise the original array. Easy!

Secondly, functional programs should be stateless, which basically means they should perform every task as if for the first time,
with no knowledge of what may or may not have happened earlier in the program’s execution (you might say that a stateless program is ignorant of the past).

Lastly, No loops, yes no for/while loop. Rather FP uses recursion to achieve the effect of looping. Not using loop because in loop a variable state gets changed
and only to avoid that we are using FP.

Example -
Let’s look at a very simple program in Java: the squares of integers.
       public class Squint {
             public static void main(String args[]) {
                      for (int i=1; i<=25; i++)
                      System.out.println(i*i);
             }
      }

Let’s look at a functional program for the squares of integers. We’ll use the language Clojure for this, though the concepts we’re going to explore work the same in any functional language.
(take 25 (squares-of (integers)))

There are three words in that program: take, squares-of, and integers. Each of those words refers to a function. Here we are passing one function as an argument to another function.


FP in Java -

Java 8 have a concept, The Lamba Expressions to achieve functional programming while not deviating from the OO design. Single method Interfaces and Anonymous inner classes are the tools on which we can do functional programming java.
How to do that. We will see that in another day, in another post.