Hello Everyone, in this series we will talk about an important Programming Paradigm, Functional Programming, and how it is applied in JavaScript. We have prepared a series of 10 articles to cover with examples what Functional Programming is. This is the first article. What are the concepts that are part of this Paradigm and how to Program in JavaScript using Functional Programming? Find the answer in this complete series.
Note to the Reader: Sometimes, dear reader, you will see the acronym TL;DR in our posts; when it is present, brace yourself, long text is coming, so the summary of the post will be placed in the TL;DR Paragraph something like Too Long Didn't Read or Text Long, Didn't Read.
TL;DR
Programming Paradigm
It is a programming style or a way of writing programs following patterns that solve specific problems; programming paradigms are at the base of the creation and functioning of programming languages. A programming language can follow a single paradigm, like Haskell, which is purely functional, or have a combination of paradigms, for example, Scala, which combines Functional Programming and Object-Oriented Programming. JavaScript is a language that also combines several paradigms and is thus called a multiparadigm programming language.
Functional Programming Origins
Functional Programming Languages - Purely Functional or Multiparadigms
Lisp, Haskell, Erlang, R, Elixir, R, Scala, JavaScript
Concepts of Functional Programming
Lambda Calculus; λ-calculus; Declarative Programming; Immutability; First-Class Functions; Pure Functions; Impure Functions; Currying;
JavaScript Frameworks and Libraries
React, Lodash, and RambdaJS
Programming Paradigm
Programming Languages are naturally created following a model of operation, a form of execution, and an environment where they are executed; Programming Paradigms comprise how a programming language works, it is the style of programming. Older languages were commonly imperative and linear, such that the program was written in the sequence it should be executed, describing step by step how a routine should function. However, with the evolution of technology, new concepts or paradigms were devised to improve code writing, code reuse both in production and maintenance of codes in programs, and then Object-Oriented Programming arises, in addition to other paradigms. The idea is always code reuse, easy maintenance, and abstraction of problems, creating increasingly intelligent and abstract routines, reused in numerous situations.
Functional Programming is yet another programming paradigm, therefore it is a way or style of programming. Some languages like Lisp, Haskell, Erlang, and Elixir are entirely built using this paradigm, so programmers in these languages need to understand the concept to make the most of the resources of FP, abbreviation for Functional Programming, although it may sound like Federal Police, it doesn’t in this text and in the following ones, FP refers to Functional Programming.
Recently, with the advent of popular libraries like React that operate on the functional programming paradigm, it becomes necessary for us to explore the concepts of how Functional Programming works, what the resources of this paradigm are, and why to use Functional Programming in web application development.
One of the great advantages of functional programming is using its resources in large-scale applications; thus, resources such as immutability, first-class functions, and many others help avoid or even minimize side effects in large-scale applications.
Libraries like React use this programming paradigm and achieve performance, organization, and scalability. In the next posts, as we understand the resources of functional programming, we will also see how to apply it in practice with powerful libraries like RambdaJS and Lodash.
If you want to get the most out of libraries like React and others that leverage the FP paradigm, these articles will help you level up your knowledge.
In this first article, we will discuss the origins of Functional Programming and some languages that are purely functional or multiparadigm. In the upcoming articles, we will detail each concept of Functional Programming.
Functional Programming Origins
The origin of FP lies in the academic world, especially in mathematics, treating programming as mathematical functions and following principles of Lambda Calculus, λ-calculus, a concept that deals with recursion and where entities can be passed as parameters and returned as values within other functions, in addition to addressing the evolution of data; in this concept, data is not altered, it evolves—this idea serves as the basis for the creation of various programming languages.
Mathematical Analysis, statistics, financial analysis, concurrent applications are among the goals of using FP. Some languages are purely functional, such as: Lisp, Haskell, Elixir, while others are multiparadigm, meaning they allow more than one paradigm: Erlang, R, Scala. JavaScript is also a multiparadigm language, and it is possible to use JavaScript functionally.
Although merely using functions in programming does not necessarily mean that one is using this paradigm, this is one of the main characteristics.
Declarative
In FP, the way of programming is declarative; for example, in JavaScript, the function array.map() will map an array, returning its data to a callback function. This function does not specify how it traverses an array element by element, how it passes the information to the callback function, so it is not imperative, like a for(let i=0; i < array.length; i++){} which in this case is imperative, meaning it explicitly states how the code will run; in FP, the programming style is always declarative.
Benefits of Functional Programming
Why use functional programming?
- Avoids side effects
- Once the data is controlled by pure functions and immutable data, we can scale the application without worrying about side effects. A component will behave as it was originally programmed.
- It is clean and not verbose
- Since declarative and immutable programming is present, there is a reduction in code writing; cleaner and more objective codes tend to be easier to maintain.
- Teamwork
- It is excellent for teamwork, as the codes developed in this paradigm always behave exactly as they were originally conceived.
Concepts of Functional Programming
In the next 9 articles, we will analyze with examples in JavaScript, what is involved in each part of Functional Programming. We will also learn to use the RambdaJS and Lodash libraries, in addition to seeing an example using React.
We hope you enjoyed this article; if you did, share it with friends and colleagues who are learning React, JavaScript, or another functional language. Take advantage of an exclusive discount for our Complete JavaScript Course with over 38 hours: https://hcode.com.br/cursos/JSFULL
See you in the next article!
Glaucio Daniel