06: 5 Java FP interview Q&As on category theory & monoids

Q01. What is a category theory from a functional programming language perspective?
A01. The category theory is all about abstracting complexities in computations. OO is good for abstracting over Data, and FP is good for abstracting over behavior.

Composition

“f” and “g” are functions, and “A” and “B” are types.

f: A => B, means the function “f” takes an input of type “A” and returns an output of type “B”.

g: B => C, means the function “g” takes an input of type “B” and returns an output of type “C”.

g compose f, means

g(f(A)): A => C,

where we pass type “A” to function “f”, and then pass the output (i.e of type B) to function “g” that returns an output of type “C”.

The “A”, “B”, and “C” themselves can be functions NOT just types like Integer, String, Person, etc. When a function takes a function as an input or returns a function as an output, it is known as a “Higher Order Function

Q02. What constitutes a category?
A02.Composition” is a requirement for something to be a category, and should satisfy the following 2 laws.

1) Asociativity: a + (b + c) = (a + b) + c
2) Identity : 0 + a = a + 0

Q03. What do you understand by the terms Monoids, Functors, Monads & Applicatives in FP?
A03. These are concepts or patterns that abstract complexities in computations, and NOT interfaces or classes. You can think of them as container type classes like List, Option, MyContainer, etc that fulfil the requirements of functors, monads, etc.

Q04. What is a monoid in functional programming?
A04. A monoid is all about combining 2 things at a time by obeying 3 rules. For example, you start with 3 numbers of type “Int” (e.g. 1,2, 3) and then combine them by performing a binary operation like “+”. i.e. (1 + 2) + 3.

Two things at a time

1) 1 + 2 = 3

2) 3 + 3 = 6

You need to obey the following 3 rules outlined below.

Rule 1: Associativity: (aka composition) When combining more than 2 things, the order of which two things you do first doesn’t matter. If we apply “+” operation to 3 numbers of type “Int”

Also,

This rule says as we are combining in pairs, the order in which we combine does not matter. The benefits of these rules are ability to divide & conquer algorithms, parallelise different operations, and perform things sequentially & incrementally.

Not all binary operations are associative. The following are not associative.

Rule 2: Identity Element: When you combine anything with an identity you get the original thing back. For additions, “0” is an identity element, for multiplication “1” is an identity, for string concatenation “” is an identity.

The “identity” depends on the binary operation you intend to perform. The purpose of this rule is to handle scenarios like a list or a string being empty. What should you do if one of the divided steps has an empty list? When you perform something sequentially and incrementally, what initial value should you start with? Here is an example in Java demonstrating associativity and identity.

The “reduce” is an aggregation operation that takes an identity value like “0”, “1”, “”, etc. It takes two elements at a time and perform a binary operation like “+”, “*”, “joining”, etc.

Rule 3: Closure: A closure is a function, whose return value depends on the value of one or more variables declared outside the function.

Each time the “addMore” function is called a new closure is created. The closure rule has the benefit that you can convert binary operations between two numbers (e.g. 1 + 2) into operations that work on Lists.

Outputs:

Q05. If you have a list of monoids, what can you do with them?
A05. A monoid is a type “T” with a binary operation that combines its values. The type “T” can be a String, Integer, etc or a custom type like “Employee”. In short, a monoid is “a binary operation and a type”.

Here is an example of a Monoid to replace sensitive data in a JSON file.

Step 1: Define the monoidic interface “MyMonoid.java” with identity & associative methods.

Step 2: Define the implementation to mask numbers that are 6 digits or more with the “MaskCardandAccountNumber.java“.

Step 3: Let’s apply to a given JSON string.

Outputs:

Top 