Explained: Catamorphism in Functional Programming with examples in javascript
Catamorphism is a concept in functional programming that refers to the process of reducing a data structure to a single value. This is typically done by applying a function (called a "catamorphism") to each element of the data structure, and then combining the results in some way.
collapsable : capable of collapsing or being collapsed.
In JavaScript, we can implement a catamorphism using the Array.reduce()
method. For example, suppose we have an array of numbers and we want to reduce it to the sum of all its elements. We can do this as follows:
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, x) => acc + x, 0);
// sum is 15
In this code, reduce()
takes two arguments: a callback function and an initial value for the accumulator (in this case, 0). The callback function itself takes two arguments: the accumulator and the current element of the array. It applies some operation to these two values (in this case, addition) and returns the result, which becomes the new value of the accumulator. This process is repeated for each element of the array, and the final value of the accumulator is the result of the catamorphism.
Another example of a catamorphism is reducing an array of objects to a single object that groups the objects by some property. For instance, suppose we have an array of objects, each of which has a name
and a value
property. We can use a catamorphism to group these objects by their name
property, like this:
const objects = [ { name: "foo", value: 1 }, { name: "bar", value: 2 }, { name: "foo", value: 3 }];
const result = objects.reduce((acc, x) => {
acc[x.name] = acc[x.name] || [];
acc[x.name].push(x.value);
return acc;
}, {});
// result is { foo: [1, 3], bar: [2] }
In this code, the callback function passed to reduce()
first checks if there is already an entry in the accumulator for the current object's name
property. If not, it creates a new entry in the accumulator and sets its value to an empty array. Then it adds the object's value
property to this array. Finally, it returns the updated accumulator. The result is an object that groups the original objects by their name
property.
Catamorphisms are a powerful tool in functional programming, as they allow us to easily and concisely reduce complex data structures to simple values. They are especially useful when combined with other functional programming techniques, such as higher-order functions and recursion.
With HOC and Recursion.
Catamorphisms can be combined with higher-order functions and recursion to perform more complex operations on data structures. For example, suppose we have a tree data structure, where each node can have any number of children. We can use a catamorphism to reduce this tree to a single value that represents the sum of all the node values in the tree.
To do this, we can define a recursive function that takes a node as input and returns the sum of all the values in that node's subtree. This function can be implemented as follows:
function sumTree(node) {
if (node.children.length === 0) {
// If the node has no children, its subtree sum is just its own value
return node.value;
} else {
// If the node has children, its subtree sum is the sum of its own value
// and the subtree sums of its children
return node.value + node.children.map(sumTree).reduce((acc, x) => acc + x, 0);
}
}
In this code, the sumTree()
function first checks if the input node has any children. If it does not, it simply returns the node's value. If it does have children, it uses Array.map()
to apply the sumTree()
function to each of its children, which calculates the subtree sum for each child. It then uses Array.reduce()
, to sum up the results of these calculations, which gives the subtree sum for the input node.
We can use this sumTree()
function as the callback function Array.reduce()
to implement a catamorphism that calculates the sum of all the values in the entire tree. For instance, suppose we have the following tree data structure:
const tree = {
value: 1,
children: [
{
value: 2,
children: [
{ value: 3, children: [] },
{ value: 4, children: [] }
]
},
{ value: 5, children: [] }
]
};
We can calculate the sum of all the values in this tree as follows:
const sum = [tree].reduce(sumTree, 0);
// sum is 15
In this code, we first wrap the tree
object in an array and pass it to Array.reduce()
. We then use our sumTree()
function as the callback, which recursively calculates the subtree sums for each node in the tree. Finally, we specify an initial value of 0 for the accumulator, which is used as the starting point for the calculation. The end result is the sum of all the values in the tree.
By combining catamorphisms, higher-order functions, and recursion, we can perform complex operations on data structures in a concise and elegant way.
With Underscore Library
In addition to using the built-in Array.reduce()
method, we can also use the _.reduce()
function from the underscore.js library to implement catamorphisms. This function is similar to Array.reduce()
, but it can be used on any data structure, not just arrays.
For example, using the tree data structure from the previous example, we can calculate the sum of all the values in the tree using _.reduce()
as follows:
const sum = _.reduce(tree, (acc, x) => {
if (x.children.length === 0) {
// If the node has no children, its subtree sum is just its own value
return acc + x.value;
} else {
// If the node has children, its subtree sum is the sum of its own value
// and the subtree sums of its children
return acc + x.value + _.reduce(x.children, (acc, y) => {
return acc + _.reduce(y, sumTree);
}, 0);
}
}, 0);
// sum is 15
In this code, we use _.reduce()
to iterate over the nodes in the tree. For each node, we use the same logic as in the previous example to calculate its subtree sum. However, since _.reduce()
can be applied to any data structure, we need to call it recursively on each child node to calculate its subtree sum. We do this by passing the sumTree()
function as the callback to a nested call to _.reduce()
.
Using the underscore.js library can make it easier to implement catamorphisms on more complex data structures, as it allows us to use a consistent syntax for iterating over and reducing any type of data structure.
Here are other blogs I have written on functional programming.