In Scala, Monads is a construction which performs successive calculations. It is an object which covers the other object. It is worth noting that here, the output of an operation at some step is an input to another computations, which is a parent to the recent step of the program stated. Monad is neither a class nor a trait, it is a concept. The maximum collections of the Scala are Monads but not all the Monads are collections, there are several Monads which are containers like Options in Scala. In short, we can say that in Scala the data types that implements map as well as flatMap() like Options, Lists, etc. are called as Monads.
Operations provided by the Monads
The objects are enclosed with Monads as it yields the following two functions:
-
unit() : It is like void in Java, it does not returns any data types.
-
flatMap() : It is similar to the map() in Scala but it returns a series in place of returning a single component.
Let’s see an example to illustrate it explicitly.
var x = Seq("Geeks", "For", "Geeks")
Let’s apply map() on the sequence given.
// Applying map()
var y = x.map(_.toUpperCase)
// Output
List(GEEKS, FOR, GEEKS)
Now, let’s apply flatMap() on the sequence given.
// Applying flatMap()
var z = x.flatMap(_.toUpperCase)
// Output
List(G, E, E, K, S, F, O, R, G, E, E, K, S)
So, when a flatMap is applied on the Sequence stated above then a List is returned where the inner grouping is removed and a sequence is generated.
Note: Collections that support map as well as flatMap are called as monadic. Now, let’s see some examples of Monads in Scala.
Examples of collection supporting map as well as flatMap.
Example :
object GfG
{
def main(args : Array[String])
{
val list 1 = List( 1 , 2 , 3 , 4 )
val list 2 = List( 5 , 6 , 7 , 8 )
val z = list 1 flatMap { q => list 2 map {
r => q + r
}
}
println(z)
}
}
|
Output:
List(6, 7, 8, 9, 7, 8, 9, 10, 8, 9, 10, 11, 9, 10, 11, 12)
Let’s see now, how the output is computed.
// Applying map() we get list like below
List(List((1+5), (1+6), (1+7), (1+8)), List((2+5), (2+6), (2+7), (2+8)),
List((3+5), (3+6), (3+7), (3+8)), List((4+5), (4+6), (4+7), (4+8)))
After evaluation we get,
List(List(6, 7, 8, 9), List(7, 8, 9, 10), List(8, 9, 10, 11), List(9, 10, 11, 12))
So, we get a List of Lists and for each operation we have a different list after applying map(), now let’s apply flatMap().
// Applying flatMap() we get a list like below
List(6, 7, 8, 9, 7, 8, 9, 10, 8, 9, 10, 11, 9, 10, 11, 12)
So, when we apply flatMap() the inner grouping is removed.
Example :
object GfG
{
def main(args : Array[String])
{
val x = ( 1 to 3 ).toList
val y = ( 1 to 7 by 2 ).toList
val z = x flatMap { s => y map {
t => s * t
}
}
println(z)
}
}
|
Output:
List(1, 3, 5, 7, 2, 6, 10, 14, 3, 9, 15, 21)
Here, List(x) = (1, 2, 3) and List(y) = (1, 3, 5, 7) then let’s see now, how the output is computed.
// Applying map() we get list like below
List(List((1*1), (1*3), (1*5), (1*7)), List((2*1), (2*3), (2*5), (2*7)),
List((3*1), (3*3), (3*5), (3*7)))
And after evaluation we get,
List(List(1, 3,, 5, 7), List(2, 6, 10, 14), List(3, 9, 15, 21))
Now, let’s apply flatMap().
// Applying flatMap() we get a list like below
List(1, 3, 5, 7, 2, 6, 10, 14, 3, 9, 15, 21)
Therefore, the internal grouping is removed.
Similar Reads
Operators in Scala
An operator is a symbol that represents an operation to be performed with one or more operand. Operators are the foundation of any programming language. Operators allow us to perform different kinds of operations on operands. There are different types of operators used in Scala as follows: Arithmeti
11 min read
ListMap in Scala
Immutable maps Implemented by using a list-based data structure. The Scala List class holds a sequenced, linear list of items. We must import scala.collection.mutable.ListMap for ListMap. ListMap collection used only for a small number of elements.Syntax: var listMapName = ListMap("k1"->"v1", "k2
3 min read
Scala Map
Map is a collection of key-value pairs. In other words, it is similar to dictionary. Keys are always unique while values need not be unique. Key-value pairs can have any data type. However, data type once used for any key and value must be consistent throughout. Maps are classified into two types: m
5 min read
Scala | map() method
A collection in Scala is a data structure which holds a group of objects. Examples of collections include Arrays, Lists, etc. We can apply some transformations to these collections using several methods. One such widely used method offered by Scala is map(). Important points about map() method: map(
2 min read
Scala Long ==(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The ==(x: Int) method is utilized to return true if this value is equal to x, false otherwise. Method Definition - def ==(x: Int): Boolean Returns - Returns true if this value is equal to than x, false othe
1 min read
Queue in Scala
A queue is a first-in, first-out (FIFO) data structure. Scala offers both an immutable queue and a mutable queue. A mutable queue can be updated or extended in place. It means one can change, add, or remove elements of a queue as a side effect. Immutable queue, by contrast, never change. In Scala, Q
3 min read
Lambda Expression in Scala
Lambda Expression refers to an expression that uses an anonymous function instead of variable or value. Lambda expressions are more convenient when we have a simple function to be used in one place. These expressions are faster and more expressive than defining a whole function. We can make our lamb
4 min read
Scala Long >=(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Int) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Int): Boolean Returns - Returns true if this value is great
1 min read
Parameterless Method in Scala
Prerequisites - Scala | Functions A parameterless method is a function that does not take parameters, defined by the absence of any empty parenthesis. Invocation of a paramaterless function should be done without parenthesis. This enables the change of def to val without any change in the client cod
2 min read
Scala Long >(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >(x: Int) method is utilized to return true if this value is greater than x, false otherwise. Method Definition - def >(x: Int): Boolean Returns - Returns true if this value is greater than x, fal
1 min read