Java: Functional Programming f(x) – Part1

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.

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)

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

Pure Function: A function which are pure and produces the same result for the same input and have no side effects.
High-Order Function: A function that takes another functions as arguments and/or returns a function as its result.
First-Order Function: A function which receive or give inputs and cannot assigned to another functions.
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

Functional Interfaces
Lambda Expressions
Method References
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!

Promoted Content

Quick SQL Query Optimization

Identify the root cause of a slow SQL query and optimize it in a fraction of the time it could have taken you.


See how!

The post Java: Functional Programming f(x) – Part1 appeared first on foojay.