# Java: Functional Programming f(x) – Part1

April 25, 2024

## Author(s)

• Senior Technical Architect hailing from India with over 17 years of experience in the field. I hold a degree in Computer Science and specialize in Java, Spring, Microservices, and AWS ... Learn more

Mathematics serves as the driving force behind significant advancements in computer science from my perspective. We deeply root the fundamental principles that we utilize in our daily programming tasks and application development in mathematical concepts. I am constantly intrigued by the opportunity to incorporate mathematical ideas into the programming languages I work with as a student of Mathematics.

Many other programming paradigms, like functional programming, take inspiration from Mathematics to address challenges related to concurrency and parallelism. Computing shifts towards utilizing multiple cores and distributed systems, which makes this increasingly important.

Java : Functional Programming

Before delving into the concept of functional programming, we must first explore Object-Oriented Programming, which revolves around the fundamental abstraction of Class/Object.

This foundational component for computation leads us to delve into imperative programming, which is all about defining what to do and how to do it

However, in Functional Programming(FP) , which revolves around the fundamental abstraction of function and we will be writing using declarative programming, which is all about defining what to do and NOT how to do it.

# Introduction

As I mentioned, Functional Programming has its roots in Mathematics and it draws most of the features from its mathematical concepts i.e., Lambda Calculus. Programming languages can view functional programming constructs as realizations or expansions of lambda calculus.

So,

## What is Lambda(λ) Calculus?

Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution.

• Lambda expression syntax with a single parameter: `λvar.expr`
For example, `λx.x * x` represents a lambda function that squares its input
• Lambda expression syntax with a multiple parameter: `λ(var1, var2).expr`
For example, `λ(x, y).x * y` represents a lambda function that multiply two numbers
• Lambda expression syntax with a code block: `λvar|(expr)`
For example, `λlist|{ list.forEach(System.out::println); }` represents a lambda function that prints each element of a list

In lambda calculus, functions can consider functions as 'first-class' entities, meaning they can use them as inputs or yield them as outputs.

Enough on Maths. Let's discuss about functional programming now...

So,

## What is Functional Programming?

In computer science, functional programming is a programming paradigm that considers computation as the evaluation of mathematical functions and emphasizes the use of expressions or declarations over statements. This approach focuses on constructing the structure and components of computer programs without modifying state or utilizing mutable data.

From my perspective, I consider it essential to evaluate the reasons for the necessity of implementing any programming language element and how it can bring advantages to various scenarios. This principle also holds true for functional programming.

Incorporating Functional Programming (FP) has a few justifications:

• Functional programming uses math-like functions without changing things or causing extra effects.
• It's good for handling many things happening at once, like with lots of computer cores or distributed systems.
• AI/ML and big data often use this kind of programming because it fits well with what they need. Examples of this include Apache Spark (using Scala) for Big Data and R for Data Science.
• Writing code this way makes it shorter, easier to understand, and more about saying what you want to happen.
• Code that is more readable and expressive is allowed by declarative programming.

### Fundamental Principles & Concepts of Functional Programming (FP)

1. Immutablity: Data is considered unchangeable, which means that once it is defined, it remains the same. This simplifies things and makes the code easier to understand.
2. Function: In functional programming, different types of functions exist, such as Pure Functions, High-Order Functions, First-Order Functions, First-Class Functions, and Monads, each representing a mapping between input and output.
3. Referential Transparency: The program works without altering how the program works by substituting values for expressions. Understanding code and allowing for optimizations are made easier by this feature.
4. Recursion: Functional programming uses recursion instead of for loops. A recursive function is a function that calls itself and keeps repeating an operation until it reaches a stopping point.
5. Functional Composition: The ability to combine functions in order to form create new functions allows us to have a modular and adaptable codebase, which in turn enables us to achieve both reusability and maintainability.
6. Lazy evaluation: Also known as nonstrict evaluation, evaluates expressions only when needed, allowing for efficient use of computer resources.

Let's discuss about type of functions in Functional Programming

A function is a fundamental concept that represents a mapping from input values to output values.

1. Pure Function: A function which are pure and produces the same result for the same input and have no side effects.
2. High-Order Function: A function that takes another functions as arguments and/or returns a function as its result.
3. First-Order Function: A function which receive or give inputs and cannot assigned to another functions.
4. First-Class Function: A function that can be assigned to a variable, passed as an argument to another function, or returned from a function

## Features of Functional Programming in Java

1. Functional Interfaces
2. Lambda Expressions
3. Method References
4. Streams(map-filter-reduce)

We will delve into the features of functional programming in great detail, providing numerous examples in the upcoming article.

# Conclusion

Functional programming, which draws inspiration from mathematics, centers around the utilization of functions as fundamental components of code. Its primary objectives are clarity and efficiency, particularly when dealing with concurrent operations or large datasets.

In the context of Java, the inclusion of lambda expressions and streams simplifies the application of functional programming principles. By delving deeper into these concepts through practical illustrations, we can gain a better understanding of how functional programming enhances the quality and scalability of code.

Stay tuned for additional insights on this subject!

## Declutter Your Code: Your Undead Code Is A Time Vampire

The average Java application contains somewhere between 10 to 50% dead code. In this webinar we'll discuss ways of monitoring JVMs across different environments to identify what runs or doesn't run in each, identify what you can get rid of, and how to work better on these larger applications.

## Related Articles

• 7 Functional Programming Techniques in Java: A Primer

There is a lot of hype around functional programming (FP) and a lot of cool kids are doing it but it is not a silver bullet.

Like other programming paradigms/styles, functional programming also has its pros and cons and one may prefer one paradigm over the other.

If you are a Java developer and wants to venture into functional programming, do not worry, you don’t have to learn functional programming oriented languages like Haskell or Clojure(or even Scala or JavaScript though they are not pure functional programming languages) since Java has you covered and this post is for you.

May 11, 2021
• 7 Reasons Why, After 26 Years, Java Still Makes Sense!

After many discussions with Java developers, combined with my personal experiences with the Java community and platform, here are the key reasons why Java developers love Java after all these years!

March 15, 2022
• 9 Outdated Ideas About Java

In this article, we want to look into some false assumptions and outdated ideas about Java based on early versions.

March 11, 2023

## Author(s)

• Senior Technical Architect hailing from India with over 17 years of experience in the field. I hold a degree in Computer Science and specialize in Java, Spring, Microservices, and AWS ... Learn more