Function program in java

Functional Programming in Java with Examples

So far Java was supporting the imperative style of programming and object-oriented style of programming. The next big thing what java has been added is that Java has started supporting the functional style of programming with its Java 8 release. In this article, we will discuss functional programming in Java 8.
What is functional programming?
It is a declarative style of programming rather than imperative. The basic objective of this style of programming is to make code more concise, less complex, more predictable, and easier to test compared to the legacy style of coding. Functional programming deals with certain key concepts such as pure function, immutable state, assignment-less programming etc.
Functional programming vs Purely Functional programming:
Pure functional programming languages don’t allow any mutability in its nature whereas a functional style language provides higher-order functions but often permits mutability at the risk of we failing to do the right things, which put a burden on us rather than protecting us. So, in general, we can say if a language provides higher-order function it is functional style language, and if a language goes to the extent of limiting mutability in addition to higher-order function then it becomes purely functional language. Java is a functional style language and the language like Haskell is a purely functional programming language.
Let’s understand a few concepts in functional programming:

  • Higher-order functions: In functional programming, functions are to be considered as first-class citizens. That is, so far in the legacy style of coding, we can do below stuff with objects.
    1. We can pass objects to a function.
    2. We can create objects within function.
    3. We can return objects from a function.
    4. We can pass a function to a function.
    5. We can create a function within function.
    6. We can return a function from a function.
  • Pure functions: A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something.
  • Lambda expressions: A Lambda expression is an anonymous method that has mutability at very minimum and it has only a parameter list and a body. The return type is always inferred based on the context. Also, make a note, Lambda expressions work in parallel with the functional interface. The syntax of a lambda expression is:
  • In its simple form, a lambda could be represented as a comma-separated list of parameters, the –> symbol and the body.
Читайте также:  Unhashable types in python

How to Implement Functional Programming in Java?

Источник

Java Functional Programming

The term Java functional programming refers to functional programming in Java. Functional programming in Java has not been easy historically, and there were even several aspects of functional programming that were not even really possible in Java. In Java 8 Oracle made an effort to make functional programming easier, and this effort did succeed to some extent. In this Java functional programming tutorial I will go through the basics of functional programming, and what parts of it that are possible in Java.

Functional Programming Basics

Functional programming contains the following key concepts:

Pure functional programming has a set of rules to follow too:

  • No state
  • No side effects
  • Immutable variables
  • Favour recursion over looping

These concepts and rules will be explained throughout the rest of this tutorial

Even if you do not follow all of these rules all the time, you can still benefit from the functional programming ideas in your applications. As you will see, functional programming is not the right tool for every problem out there. Especially the idea of «no side effects» makes it hard to e.g. write to a database (that is a side effect). You need to learn what problems functional programming is good at solving, and which it is not.

Functions as First Class Objects

In the functional programming paradigm, functions are first class objects in the language. That means that you can create an «instance» of a function, as have a variable reference that function instance, just like a reference to a String, Map or any other object. Functions can also be passed as parameters to other functions.

In Java, methods are not first class objects. The closest we get is Java Lambda Expressions. I will not cover Java lambda expressions here, as I have covered them in both text and video in my Java Lambda expression tutorial.

Pure Functions

A function is a pure function if:

  • The execution of the function has no side effects.
  • The return value of the function depends only on the input parameters passed to the function.

Here is an example of a pure function (method) in Java:

public class ObjectWithPureFunction < public int sum(int a, int b) < return a + b; >>

Notice how the return value of the sum() function only depends on the input parameters. Notice also that the sum() has no side effects, meaning it does not modify any state (variables) outside the function anywhere.

Contrarily, here is an example of a non-pure function:

public class ObjectWithNonPureFunction < private int value = 0; public int add(int nextValue) < this.value += nextValue; return this.value; >>

Notice how the method add() uses a member variable to calculate its return value, and it also modifies the state of the value member variable, so it has a side effect.

Higher Order Functions

A function is a higher order function if at least one of the following conditions are met:

  • The function takes one or more functions as parameters.
  • The function returns another function as result.

In Java, the closest we can get to a higher order function is a function (method) that takes one or more lambda expressions as parameters, and returns another lambda expression. Here is an example of a higher order function in Java:

public class HigherOrderFunctionClass < public IFactory createFactory(IProducer producer, IConfigurator configurator) < return () -> < T instance = producer.produce(); configurator.configure(instance); return instance; >> >

Notice how the createFactory() method returns a lambda expression as result. This is the first condition of a higher order function.

Notice also that the createFactory() method takes two instances as parameters which are both implementations of interfaces ( IProducer and IConfigurator ). Java lambda expressions have to implement a functional interface, remember?

Imagine the interfaces looks like this:

public interface IFactory
public interface IProducer
public interface IConfigurator

As you can see, all of these interfaces are functional interfaces. Therefore they can be implemented by Java lambda expressions — and therefore the createFactory() method is a higher order function.

Higher order functions are also covered with different examples in the text about Higher Order Functions

No State

As mentioned in the beginning of this tutorial, a rule of the functional programming paradigm is to have no state. By «no state» is typically meant no state external to the function. A function may have local variables containing temporary state internally, but the function cannot reference any member variables of the class or object the function belongs to.

Here is an example of a function that uses no external state:

Contrarily, here is an example of a function that uses external state:

This function clearly violates the no state rule.

No Side Effects

Another rule in the functional programming paradigm is that of no side effects. This means, that a function cannot change any state outside of the function. Changing state outside of a function is referred to as a side effect.

State outside of a function refers both to member variables in the class or object the function, and member variables inside parameters to the functions, or state in external systems like file systems or databases.

Immutable Variables

A third rule in the functional programming paradigm is that of immutable variables. Immutable variables makes it easier to avoid side effects.

Favour Recursion Over Looping

A fourth rule in the functional programming paradigm is to favour recursion over looping. Recursion uses function calls to achieve looping, so the code becomes more functional.

Another alternative to loops is the Java Streams API. This API is functionally inspired.

Functional Interfaces

A functional interface in Java is an interface that only has one abstract method. By an abstract method is meant only one method which is not implemented. An interface can have multiple methods, e.g. 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

Here is another example of a functional interface with a default method and a static method too:

public interface MyInterface2 < public void run(); public default void doIt() < System.out.println("doing it"); >public static void doItStatically() < System.out.println("doing it statically"); >>

Notice the two methods with implementations. This is still a functional interface, because only run() is not implemented (abstract). However, if there were more methods without implementation, the interface would no longer be a functional interface, and could thus not be implemented by a Java lambda expression.

Источник

Оцените статью