Friends of OpenJDK Today

Java: Functional Programming f(x) – Part1

April 25, 2024

Author(s)

  • Avatar photo
    Mahendra Rao B

    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.

Functional Programming
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.

So let's discuss about functional programming and its advantages in this article.

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!

Sponsored Content

Stable, Secure, and Affordable Java

Azul Platform Core is the #1 Oracle Java alternative, offering OpenJDK support for more versions (including Java 6 & 7) and more configurations for the greatest business value and lowest TCO.

Download Here!

Related Articles

View All

Author(s)

  • Avatar photo
    Mahendra Rao B

    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

Comments (1)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Java Weekly, Issue 540 | Baeldung

[…] >> Java: Functional Programming f(x) – Part1 [foojay.io] […]

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard