Functional Programming

Overview: Java 8 introduced functional programming. Let’s understand what it is. Basics of functional programming: it’s a paradigm that enables concurrent usage of code. Lambda expressions are pillars of functional programming. 

Pure Function: Pure functions should follow these rules:

  • No state: it means a function should not refer any member of a class or an object.
  • No side effects: a function can not change the state outside of the function.
  • Immutable variables: use immutable variables.
  • Prefer recursion over looping. Another option is to use stream API.

First class function: A first class function can be passed as a parameter to a function. It’s like a one-time use function with a function. In Java, it’s achieved using a lambda expression.

High order function: It takes a function as a parameter or returns a function. In Java, it’s achieved using a lambda expression.

Functional interface: An interface is a functional interface if it has a single abstract (unimplemented) method. This interface could also have a default method and a static method.

Functional composition: it is a technique to combine multiple functions into a single function.

Imperative versus Recursive function: Imperative is a simple way of writing logic. Recursion is a way to call function in a nested way. Recursion is of two types: Tail and Head Recursion. Tail recursion has a recursive method call at the end of the logic in a method. Head recursion has a recursive method at the beginning of the logic in a method.

Parallelism: In this concept, we split the task into multiple sub tasks. At the end, we combine the results of subtasks for the final result.

Monads: A monad is a design pattern that helps to represent a missing value.

Java Stream API: Java Stream API is a way that makes operations on data sources easier and convenient. It does not modify underlying data sources. Some examples of common stream operations are forEach, map, collect, filter, findFirst, flapMap, etc.

Lambdas: A Java lambda expression is a short code which takes input parameters and returns a value. These are like methods but without a name. Java Lambda expression provides the way to implement a functional interface.

References:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s