What is functional programming? Functional programming is a programming paradigm — a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data
What is a pure function and how do we know if something is a pure function? It returns the same result if given the same arguments (it is also referred as deterministic) It does not cause any observable side effects It returns the same result if given the same arguments Imagine we want to implement a function that calculates the area of a circle. An impure function would receive radius as the parameter, and then calculate radius radius PI:
let PI = 3.14;
const calculateArea = (radius) => radius * radius * PI;
calculateArea(10); // returns 314.0
What are the benefits of a pure function? The code’s definitely easier to test. We don’t need to mock anything. So we can unit test pure functions with different contexts. 1. Given a parameter A → expect the function to return value B 2. Given a parameter C → expect the function to return value D
What is immutability? Unchanging over time or unable to be changed. When data is immutable, its state cannot change after it’s created. If you want to change an immutable object, you can’t. Instead, you create a new object with the new value.
What is Referential transparency?
Let’s implement a square function: const square = (n) => n * n;
This pure function will always have the same output, given the same input.
square(2); // 4
square(2); // 4
square(2); // 4
Passing 2 as a parameter of the square function will always returns 4. So now we can replace the square(2) with 4. That’s it! Our function is referentially transparent.
Basically, if a function consistently yields the same result for the same input, it is referentially transparent.
pure functions + immutable data = referential transparency
With this concept, a cool thing we can do is to memoize the function.
var module = require('module_name');
As per above syntax, specify the module name in the require() function. The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns.
const moduleName = require(‘./filename.js’); You don’t have to add the “.js” extension, Node.js can still load your local module without it as we have learned.
const moduleName = require(‘./filename’); Core ModulesPermalink These are modules that come with Node.js by default. You do not have to download them in your project.
Some of the most popular and frequently used core modules are fs, os, path, etc.
Importing core modules
To import a core module, you have to use the require() method with the core module’s name passed as the argument.
const fileSystem = require(“fs”); Third-Party ModulesPermalink Third-party modules are modules that are downloaded with a package manager such as npm. These modules are usually stored in the node_modules folder.
You can install third-party modules globally or locally in your project.
Examples of third party modules are express, mongoose, react, etc.
Importing Third-Party Modules
To import a third-party module, you have to use the require() method that takes the third-party module’s name as an argument.
const fileSystem = require("express");