Beginners guide to functional Programing with Javascript Part 2

Beginners guide to functional Programing with Javascript Part 2

·

5 min read

In part 2 of our series on functional programming for beginners, we will continue to explore more concepts of functional programming.

Pure Functions

In functional programming, a pure function is a function that always returns the same output for a given input, and that has no side effects. This means that a pure function does not modify any external state, such as global variables or the properties of an object, and does not produce any observable side effects, such as printing to the screen or performing an I/O operation. Pure functions are important in functional programming because they are predictable and have no hidden side effects, which makes them easy to reason about and test.

To illustrate the use of pure functions in JavaScript, consider the following example:

// Define a pure function that adds two numbers
function add(x, y) {
return x + y;
}

// Define a pure function that squares a number
function square(x) {
return x * x;
}

// Define a pure function that calculates the square of the sum of two numbers
function squareOfSum(x, y) {
// Use the add and square functions to calculate the square of the sum
return square(add(x, y));
}

// Calculate the square of the sum of 3 and 4
console.log(squareOfSum(3, 4)); // Output: 49

In this example, the add, square, and squareOfSum functions are all pure functions. They always return the same output for a given input, and they have no side effects. This makes them easy to reason about and test, and allows them to be composed and combined in powerful ways.

Function Composition

Function composition is a technique in functional programming that involves combining multiple functions to create a new function. This allows for the creation of complex behavior by combining simple, reusable functions in a modular and declarative way.

To illustrate the use of function composition in JavaScript, consider the following example:

// Define a pure function that adds two numbers
function add(x, y) {
return x + y;
}

// Define a pure function that squares a number
function square(x) {
return x * x;
}

// Define a function that composes two functions
function compose(f, g) {
// Return a new function that applies f to the result of g
return function(x) {
return f(g(x));
};
}

// Use the compose function to create a new function that
// squares the sum of two numbers
const squareOfSum = compose(square, add);

// Calculate the square of the sum of 3 and 4
console.log(squareOfSum(3, 4)); // Output: 49

In this example, the compose function takes two functions, f and g, and returns a new function that applies f to the result of g. This allows for the creation of a new function, squareOfSum, by composing the square and add functions. This demonstrates how function composition can be used to create complex behavior by combining simple, reusable functions in a modular and declarative way.

Arity

Another important concept in functional programming is arity, which refers to the number of arguments that a function takes. Functions with a fixed arity always take the same number of arguments, and functions with a variable arity can take a varying number of arguments. In functional programming, it is often useful to have functions with a fixed arity, because this allows for the creation of more predictable and reusable code.

To illustrate the use of fixed arity functions in JavaScript, consider the following example:

// Define a function with a fixed arity of 2 that calculates the square of the sum of two numbers
function squareOfSum(x, y) {
// Use the add function to calculate the sum of x and y
const sum = add(x, y);

// Use the square function to calculate the square of the sum
return square(sum);
}

// Define a pure function that adds two numbers
function add(x, y) {
return x + y;
}

// Define a pure function that squares a number
function square(x) {
return x * x;
}

// Calculate the square of the sum of 3 and 4
console.log(squareOfSum(3, 4)); // Output: 49

In this example, the squareOfSum function has a fixed arity of 2, which means that it always takes exactly two arguments. This allows for the creation of more predictable and reusable code, because the squareOfSum function can only be called with exactly two arguments, and it cannot be passed a variable number of arguments.

Currying

Another technique that is often used in functional programming is currying, which is the process of transforming a function with multiple arguments into a sequence of functions with a single argument. This allows for the creation of more reusable and composable functions, because each function in the sequence only takes a single argument.

To illustrate the use of currying in JavaScript, consider the following example:

// Define a function that takes multiple arguments and
// returns a function that takes the remaining arguments
function curry(func) {
// Convert the arguments object into an array
const args = Array.from(arguments);

// Remove the first argument (the function) from the array
args.shift();

// Return a function that takes a single argument
return function(x) {
// Append the argument to the array of arguments
args.push(x);
};
}
// Use the curry function to create a curried version of the squareOfSum function
const curriedSquareOfSum = curry(squareOfSum);

// Calculate the square of the sum of 3 and 4 by passing all of the arguments
// at once to the curried function
console.log(curriedSquareOfSum(3)(4)); // Output: 49

In this example, the curry function takes a function and any number of arguments, and returns a new function that takes a single argument. This allows for the creation of a curried version of the squareOfSum function, where each argument can be passed to a separate function in the curried function sequence. This demonstrates how currying can be used to create more reusable and composable functions in functional programming.

Summary

In summary, functional programming is a programming paradigm that emphasizes the use of pure functions, function composition, fixed arity, and currying to create modular and declarative code. JavaScript has many of the features that are commonly associated with functional programming languages, such as first-class functions and support for higher-order functions, which means that it is possible to write JavaScript code in a functional style.