Functional Programming in JS
Introduction
In this blog I am going to talk about functional programming and best practices we use when working with programming languages. But before that lets cover some fundamental concepts and terminologies.
Paradigm: In science and philosophy, a paradigm is a distinct set of concepts or thought patterns, including theories, research methods, postulates, and standards for what constitutes legitimate contributions to a field.
Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.
Common programming paradigms
- imperative in which the programmer instructs the machine how to change its state,
- procedural which groups instructions into procedures,
- object-oriented which groups instructions with the part of the state they operate on,
- declarative in which the programmer merely declares properties of the desired result, but not how to
compute it
- functional in which the desired result is declared as the value of a series of function applications,
- logic in which the desired result is declared as the answer to a question about a system of facts and rules,
- mathematical in which the desired result is declared as the solution of an optimization problem
Declarative Programming Paradigm
In computer science, declarative programming is a programming paradigm—a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.
Many languages that apply this style attempt to minimize or eliminate side effects by describing what the program must accomplish in terms of the problem domain, rather than describe how to accomplish it as a sequence of the programming language primitives (the how being left up to the language's implementation). This is in contrast with imperative programming, which implements algorithms in explicit steps. Common declarative languages include those of database query languages (e.g., SQL, XQuery), regular expressions, logic programming, functional programming, and configuration management systems.
Functional Programming Paradigm
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map values to other values, rather than a sequence of imperative statements which update the running state of the program.
Common functional programming practices
- functions are treated as first-class citizens
- they can be bound to names (including local identifiers)
- passed as arguments
- returned from other functions, just as any other data type can
This allows programs to be written in a declarative and composable style, where small functions are combined in a modular manner.
pure functional programming
- When a pure function is called with some given arguments, it will always return the same result, and cannot be affected by any mutable state or other side effects
- Do not cause any immutability (side-effect, like console logging, writing on a file, querying/updating DOM, fetch call)
Higher-order functions
- functions that can either take other functions as arguments or return them as results.
- Supports currying and function composition
Functional Programming practices in JS
Imperative Style programming
const doubleMap = numbers => {
const doubled = [];
for (let i = 0; i < numbers.length; i++) {
doubled.push(numbers[i] * 2);
}
return doubled;
};
console.log(doubleMap([2, 3, 4])); // [4, 6, 8]
Declarative Style programming
const doubleMap = numbers => numbers.map(n => n * 2);
console.log(doubleMap([2, 3, 4])); // [4, 6, 8]
Conclusion
Functional programming favors:
- Declarative rather than imperative code
- Pure functions instead of shared state & side effects
- Immutability
- Function composition or currying using Higher order function
- Using generic, reusable utilities that use higher order functions to act on many data types
- Expressions over statements