Functional Programming — It Is A Way Of Life

Functional Programming — It Is A Way Of Life

What are the main aspects of Functional Programming? How is it different from our regular OOPS?

Functional Programming has been thriving in recent times. It is actually a way of life. The main challenge here is to unlearn the OOPS way of life and then learn the Functional Programming way. Once you learn it, it makes life so easy, it’s more concise, predictable, and less complex. Testing the code becomes seamless.

There are two types of programming — Imperative and Declarative, Functional programming is a declarative way of programming. Functional programming is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. The application state flows through pure functions.

So let’s look into what are the things we need to learn to adapt to the functional way of life.

Imperative vs Declarative Approach

We’ll take an example and see how we can code for it in both approaches

public class App {
        public static void main(String[] args) {
            List<Person> people = Arrays.asList(new Person("Vinesh", Gender.MALE), 
                               new Person("Neha", Gender.FEMALE),
                    new Person("Sameer", Gender.MALE), new Person("Rakshith", Gender.MALE),
                    new Person("Swaroop", Gender.MALE), new Person("Saurabha", Gender.FEMALE),
                    new Person("Naina", Gender.FEMALE));
        }

        static class Person {
            private final String name;
            private final Gender gender;

            public Person(String name, Gender gender) {
                this.name = name;
                this.gender = gender;
            }
            @Override
            public String toString() {
                return "Person [name=" + name + ", gender=" + gender + "]";
            }
        }
        enum Gender { MALE, FEMALE }
    }

Let’s say we have the above code and we want to filter all females from the list, let’s first print all females using both imperative & declarative method.

// Imperative approach
    for (Person person : people) {
        if (Gender.FEMALE.equals(person.gender)) {
            System.out.println(female);
        }
    }

// Declarative approach people.stream().filter(person -> Gender.FEMALE.equals(person.gender)) .forEach(System.out::println);

```

The imperative code is too big for a simple task whereas the declarative method achieves it with just a single line.

object-oriented coding java functional-programming programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Find Ulimit For user on Linux

Explains how to find ulimit values of currently running process or given user account under Linux using the 'ulimit -a' builtin command.

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS - We are going to build a full stack Todo App using the MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This is the last part of three-post series tutorial.

Docker Applikationen mit Visual Studio Code debuggen

Mit dem integrierten Debugger von Visual Studio Code lassen sich ASP.NET Core bzw. .NET Core Applikationen einfach und problemlos debuggen. Der Debugger unterstützt auch Remote Debugging, somit lassen sich zum Beispiel .NET Core Programme, die in einem Docker-Container laufen, debuggen.

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Creating RESTful APIs with NodeJS and MongoDB Tutorial - Welcome to this tutorial about RESTful API using Node.js (Express.js) and MongoDB (mongoose)! We are going to learn how to install and use each component individually and then proceed to create a RESTful API.

systemctl List All Failed Units/Services on Linux

Explains how to use the systemctl command to list all failed units or services on Debian, Ubuntu, CentOS, Arch, Fedora, and other Linux distros.