-
Notifications
You must be signed in to change notification settings - Fork 0
Module 4
Functional programming is a programming paradigm that emphasizes on the use of pure functions and immutable data. In functional programming, functions are treated as first-class citizens, which means they can be assigned to variables, passed as arguments to other functions, and returned as values from functions.
Functional programming languages are designed to support and encourage the use of functional programming techniques, but functional programming can also be used in imperative programming languages like JavaScript, Python, and Ruby.
Pure functions A pure function is a function that, given the same input, will always return the same output, and has no observable side effects. A pure function does not modify any external state, and does not depend on any external state or mutable data.
// Example of a pure function in JavaScript
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // Output: 5
console.log(add(2, 3)); // Output: 5
In the above example, the add function is a pure function because it takes two arguments and returns their sum without modifying any external state.
Immutable data is data that cannot be changed after it is created. In functional programming, immutable data is preferred over mutable data, because it makes it easier to reason about the behavior of a program, and eliminates the possibility of unwanted side effects.
// Example of immutable data in JavaScript
const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = numbers.map((number) => number * 2);
console.log(numbers); // Output: [1, 2, 3, 4, 5]
console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
In the above example, the numbers array is immutable because it is not modified, and a new array doubledNumbers is created using the map method.
Function composition is the process of combining two or more functions to produce a new function. In functional programming, function composition is used extensively to build complex programs from smaller, reusable functions.
// Example of function composition in JavaScript
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
const addAndMultiply = (a, b, c) => multiply(add(a, b), c);
console.log(addAndMultiply(2, 3, 4)); // Output: 20
In the above example, the addAndMultiply function is created by composing the add and multiply functions.
A higher-order function is a function that takes one or more functions as arguments, or returns a function as its result. Higher-order functions are a key feature of functional programming, and enable powerful abstractions and code reuse.
// Example of a higher-order function in JavaScript
function mapArray(array, fn) {
const result = [];
for (let i = 0; i < array.length; i++) {
result.push(fn(array[i]));
}
return result;
}
console.log(mapArray([1, 2, 3, 4, 5], (number) => number * 2)); // Output: [2, 4, 6, 8, 10]
In the above example, the mapArray function is a higher-order function because it takes an array and a function as arguments, and returns a new array after applying the function to each element of the original array.
- Your functions should accept at least one argument.
- Your functions should return data, or another function.
- Don’t use loops. Functional programming (FP) is an approach to software development that uses pure functions to create maintainable software. In other words, building programs by applying and composing functions.
- In functional programming, we can’t modify a variable after it’s been initialized.
- We can create new variables, but we can’t modify existing variables.
- This really helps to maintain state throughout the runtime of a program.
- Once we create a variable and set its value, we can have full confidence knowing that the value of that variable will never change.
- Pure functions are easier to understand because they don’t change any states and depend only on the input given to them.
- The ability of functional programming languages to treat functions as values and pass them to functions as parameters make the code more readable and easily understandable.
- Testing and debugging is easier. Since pure functions take only arguments and produce output. They use immutable values, so it becomes easier to check some problems in programs written uses pure functions.
- It is used to implement concurrency/parallelism because pure functions don’t change variables or any other data outside of it.
- Sometimes writing pure functions can reduce the readability of code.
- Writing programs in recursive style instead of using loops can be bit intimidating.
- Writing pure functions are easy but combining them with the rest of the application and I/O operations is a difficult task.
- Immutable values and recursion can lead to decrease in performance.
It is used in mathematical computations. It is needed where concurrency or parallelism is required.
Fact: Whatsapp needs only 50 engineers for its 900M users because Erlang is used to implement its concurrency needs. Facebook uses Haskell in its anti-spam system.
Function composition allows you to combine pure functions to create more complicated ones. The same principle applies in mathematics: the result of one function continues as the argument of the following function, and the result of the last function is the result of the whole.
In coding, we can combine multiple steps into a single code line or into a new function.