Understanding Functional Programming

Reading Time: 4 minutes

This blog describes all about Functional Programming. With this blog any novice will be able to understand benefits of using functional programming and will able to apply it in their code base to increase performance. So lets begin with introduction of functional programming.

Introduction

Functional programming is a type of paradigm or pattern which is defined in computer science and here we do everything with the help of functions and the basic building blocks are also functions only.

Functional programming is basically a process of building software by composing pure functions. Pure functions are the functions which gives same input and always returns a same output. It does not have any side effects.

We will learn later about pure functions in detail with example.

Before understanding the functional programming lets first understand about functions.

Functions

Functions are fundamental to code organization as they exist in all higher order programming languages.

With Side effects

With no side effects

Functions in functional programming are also called as First class citizens.There are some parameters which makes functions as first class citizens.

1. Firstly , Functions are like first class data for instance literals and objects
2. Secondly , Function can take function as argument.
3. Thirdly, Function can return function as result
4. Most importantly is partial application of function

Fundamental Principles and Concepts of Functional programming

1. Higher order functions

First-class functions(function as a first-class citizen) means you can assign functions to variables, pass a function as an argument to another function or return a function from another.

A function can be considered as a higher-order-function only if it takes one or more functions as parameters or if it returns another function as a result.

2. Pure functions

Pure functions basically returns the value based on the arguments , For example, we can trust that 2 + 2 is always 4 and 3 x 3 is always 9

Example:

``````public int maxNumber(int a, int b){
if(a>b){
return a;
}
return b;
} ``````

For a=8 and b=15 the maxNumber function will always return 15.

So, pure function behaves like below diagram where some input is in the form of arguments and after some logical operation output will be generated,

3. Immutability

This feature defines that once a thing is defined then it can not be modified , we all know that java already have several built in immutable types.

Functional Interfaces in java

In Functional programming in java functional interface are the core part , when we talk about Functional programming.

A functional interface in Java is an interface that only has one abstract method. One abstract method means is only one method which is not implemented. An interface can have multiple methods for example default methods and static methods, both with implementations, but as long as the interface only has one method that is not implemented, the interface is considered a functional interface.

Here is an example of a functional interface:

``````public interface MyInterface {
public void run();
}
``````

Since there is quite a large amount of information about functional programming here we will learnlambda expressions.

Lambdas expressions

A lambda expression consists of two parts separated by the “->” operator. The left side of the operator represents the list of input parameters, while the right side represents the function body.

Lambda expression syntax:

```(parameters) -> { statements };
```

Let’s see an example to understand how can lambda expressions used in practice. For a better understanding we will start a piece of information written in Lambda style.

``````interface Condition {
boolean test(Book book);
}
public static void printConditionally(List<Book> books, Condition condition) {
for (Book book : books) {
if (condition.test(book)) {
System.out.println(book);
}
}
}
public static void main(String[] args) {
List<Book> books = Arrays.asList(new Book("Destination happiness", 2017),
new Book("30 Days", 1998),
new Book("So sad today", 2000),
new Book("Skippy Diesenn”,1986)
new Book("Difficult women", 1996),
new Book("Homesick for Another Wolrd", 1996));

//sort list by title
Collections.sort(book, (o1, o2) -> o1.getTitle().compareTo(o2.getTitle()));

//print all books that have title beginning with 'S'
printConditionally(books, book -> book.getTitle().startsWith("S"));

//prints all books that have year of publication <2000
printConditionally(books, book -> book.getYearOfPublication() < 2000);
}``````

Advantages and disadvantages of Functional Programming

Advantages:

1. This programming helps to solve the problems in effective way.
2. It improves modularity.
3. It allow us to implement lambda calculus in our program to solve complex problems
4. Some programming languages support nested functions which improve maintainability of the code (currying)
5. It reduces complex problems into simple pieces

Disadvantages:

1. For beginners, it is difficult to understand. So it is not a beginner friendly paradigm approach for new programmers
2. Maintenance is difficult during the coding phase when the project size is large
3. Re usability in Functional programming is a tricky task for developers

Conclusion

we went through the basics of functional programming. We covered the fundamental principles and how we can adopt them in Java. I would be explaining more about Java Functional Interface API for Lambda expressions soon.

References

https://www.tutorialspoint.com/functional_programming_with_java

https://www.baeldung.com/java-functional-programming

http://tutorials.jenkov.com/java-functional-programming