13: Q81 – Q83 Scala monoids interview Q&As

Monoids, Functors, Monads, Applicatives, etc are concepts or patterns that abstract complexities in computations, and NOT traits or classes. You can think of them as container type classes like List[T], Option[T], Futures[T], MyContainer[T], etc that fulfill requirements of functors, monads, etc. Let’s start with monoids first.

Q81. What is a monoid in functional programming?
A81. 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”


This rule says as we are combining in pairs, the order in which we combine does not matter. The benefits of this rules are ability to divide & conquer algorithms, parallelize 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 any thing with a “Zero” (aka identity) element like “0” for addition, “1” for multiplication, empty string or list for concatenation, you get the original thing back.

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? E.g.

Can be simplified with wild place holder _

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

So, with a list of monoids you can combine or concatenate them together. You “reduce”, “fold”, etc. For example,


Q83. Can you give some examples of monoids in Scala?

Example 1:

Note that “:::, ++“, etc are functions in the collection classes that can be written as List(1,5).:::(List(5,6)) OR List(1,5) ::: List(5,6) without the “.” notation.

Example 2:

Create a MyMonoid trait with two functions – combine (i.e. combine) & empty (i.e. identity)

Use the trait, and create an instance of the anonymous class “Function3$$anon$1” assigned to “listMonoid”.

Here is the code demonstrating a Monoid that combines Lists.


Example 3:

Here is the code demonstrating a Monoid that combines Lists. An instance of the anonymous class “Function3$$anon$1” assigned to “stringMonoid”.

Example 4: with Option

Example 5: with implicits

Example 6: with implicits

Apply some functions like isOdd, isEven, isPrim, etc to a list of numbers.


Categories Menu - Q&As, FAQs & Tutorials