What is Functional Programming?

What is Functional Programming?

Most of what I will discuss in this article is knowledge accumulated from reading, “Functional Programming in JavaScript”, by Luis Atencio. In simple terms, functional programming is a software development style that places a major emphasis on the use of functions.

Most of what I will discuss in this article is knowledge accumulated from reading, “Functional Programming in JavaScript”, by Luis Atencio. Let’s dig right in…

What is functional programming?

In simple terms, functional programming is a software development style that places a major emphasis on the use of functions. You might say, “Well, I already use functions daily, what’s the difference?” Well, it’s not a matter of just applying functions to come up with a result. The goal, rather, is to abstract control flows and operations on data with functions in order to avoid side effects and reduce mutation of state in your application.

Abstract Control Flows

To understand what abstract control flows means, we need to first look at a model of programming called Imperative **or Procedural Programming**. Imperative programming treats a computer program as merely a sequence of top-to-bottom statements that change the state of the system in order to compute a result.

Imperative programming tells the computer, in great detail, how to perform a certain task (looping through and applying a given formula to each item in an array for instance). This is the most common way of writing code. Here is an example.

var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for(let i = 0; i < array.length; i++) {
    array[i]= Math.pow(array[i], 2);
}
array;
//-> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Functional programming, on the other hand, falls under the umbrella of Declarative Programming: it’s a type of programming that expresses a set of operations without revealing how they’re implemented or how data flows through them.

Declarative programming separates program description from evaluation. It focuses on the use of expressions to describe what the logic of a program is without necessarily specifying its control flow or state changes.

You only need to be concerned with applying the right behavior at each element and give-up control of looping to other parts of the system. For example, letting Array.map() do most of the heavy lifting in the task we did earlier, would be considered a more functional approach.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(num => Math.pow(num, 2));
//-> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

With this approach, we abstract our loops with functions, and compared to the previous example, you see that this code frees you from the responsibility of properly managing a loop counter and array index access. Put simply, the more code you have, the more places there are for bugs to occur. Also, standard loops aren’t reusable artifacts unless they’re *abstracted *with functions.

Ideally, manual loops should be removed completely from your code in favor of first-classhigher-order functions like mapreduce and filter, which accept functions as parameters so that your code is more reusable, extensible, and declarative.

javascript functional-programming web-development programming software-development 100daysofcode code-quality beginners

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

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

Software Developer vs Web Developer | Difference Web Developer & Software Developer

Software Developer vs Web Developer | Difference Web Developer & Software Developer | Software development and web development are normally used interchangeably. Although both include development, a software developer has different responsibilities from that of a web developer.

Functional Programming Techniques in Java with Examples

In this article, I will write about how to write code using Java’s API for functional programming. In the end, we will write our own stream API so we can understand how to implement a functional programming style in Java.

Functional Programming

Functional Programming: Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.