Izac Wiki

Functional Programming

Functional Programming

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

Pure Functions

A pure function is a function that always returns the same result if the same arguments are passed in. It does not depend on any state, or data, change during a program’s execution. It must also not produce any observable side effects.

      
        
// Pure function
function add(a, b) {
return a + b;
}
// Impure function
function add(a, b) {
return a + b + new Date().getSeconds();
}

Higher Order Functions

A higher order function is a function that takes a function as an argument, or returns a function.

      
        
function add(a, b) {
return a + b;
}
function higherOrderFunction(a, b, callback) {
return callback(a, b);
}
higherOrderFunction(1, 2, add); // 3

First Class Functions

A programming language is said to have first-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

      
        
function add(a, b) {
return a + b;
}
const subtract = function(a, b) {
return a - b;
}
const multiply = (a, b) => {
return a * b;
}
const divide = (a, b) => a / b;

Pure Functions

A pure function is a function that always returns the same result if the same arguments are passed in. It does not depend on any state, or data, change during a program’s execution. It must also not produce any observable side effects.

      
        
// Pure function
function add(a, b) {
return a + b;
}
// Impure function
function add(a, b) {
return a + b + new Date().getSeconds();
}

Immutability

An immutable object is an object that can’t be modified after it’s created. Conversely, a mutable object is any object which can be modified after it’s created.

      
        
const person = {
name: "John",
age: 30
};
// Mutable
person.age = 31;
// Immutable
const newPerson = Object.assign({}, person, { age: 31 });
const newPerson = { ...person, age: 31 };

Recursion

Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a result. Most loops can be rewritten in a recursive style, and in some functional languages this approach to looping is the default.

      
        
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
factorial(3); // 6