Skip to main content

Functional Programming Introduction

 Greetings!

One of the biggest changes happened to Java is, addition of Functional programming in Java 8. It is great because now we can use imperative, procedural, object-oriented, and functional techniques in Java.

Java added functional programming to the language using Lambda expressions, Stream, Optional, default methods in interface, static methods in interface, functional interface.

Let's dive into important concepts in functional programming.


First of all, What is a Function?


f(x) = y

That is a function which gives us 'y' for a given 'x'.

Let's see an easy one.


f(x) = x + 1

This will give us the next value for a given 'x'. So what is the big deal here?

  • Immutable, ie no mutation of data.
  • No sate.
  • No side effects.
  • Always gives the same output for the same input.


Important concepts

  • Pure function
  • First class function
  • Higher order function
  • Referential transparency
  • Lazy evalutation
  • Closure
  • Currying
  • Functor
  • Monad
  • Composition
  • Chanining/ Fluent interfaces


Pure Function

A functions is said a pure function when;

  • Referential transparency:- the same input generates the same output.
  • No side effect:- no state changes, no I/O


Function fx = x -> x + 1;


First Class Function

Functions can be assigned to variable, pass as arguments, return by other functions. In Java methods are not first class functions. The closest we get is lambda expressions.


Function<Integer, Integer> fx = x -> x + 1;


Higher Order Function

A function which accpets other functions as arguments or return function as its result or do the both is said a higher order function.


Function> hx = x -> y -> x + y;


Referential Transperancy

A functions returns the same result for the same argument. This is because it depends only on the input.


Lazy Evaluation

Lazy evaluation means, fucntion is not evaluated untill necessary.


Closure

A closure is basically a function which remembers it is context.


Function<String, Function<String,String> fx = x -> {
  String outer = "Outer:" + x;
  return y -> {
    String inner = "Inner:" + y;
    return outer + ", " + inner;
  };
};


Currying

Currying is the proccess of transforming a function that takes multiple arguments into multiple functions which takes only one argument.

Number of arguments a function takes is called 'aritiy'.  So if the arity is 'n' then it creates n unary functions.


Function>Integer, Function> fx = x -> y -> x + y;


Functor

Functor is a container that can hold a value. It also should implement 'map' contract.


Monad

Monad is a special type of Functor.  A Functor which has chain/flatMap function is called a Monad. Stream, Optional are Monads in Java.


Function Composition

In mathematics function composition is denoted as:

h(x) = g(f(x))

Function composition is combining two or more functions to make new function.


Function<Integer, Integer> next = x -> x + 1;
Function<Integer, Integer> square = x -> x * x;
Function<Integer, Integer> composed = square.compose(next);


Function chaining/ Fluent interfaces


OOP vs FP

There is no such a thing. Both work nicely together.