Updated March 31, 2023
Introduction to Scala Comprehensions
Scala Comprehensions are features of Scala having the structure for(enumerators) yield e that evaluates a body for enumerators and yield the result for that. It has a generator that is a new variable or a filter on which scala comprehensions work and produce the result. Scala comprehensions provide and concise way of evaluating a piece of code in scala programming. It has a generator, Filters, and definitions through which it works and produces the desired result with the yield function. Let us see how Scala comprehensions exactly work and its benefit in Scala Programming.
Working of Scala Comprehensions
Scala Comprehension works as with the structure for making it mixed with various operations like Map, Flat Map, filter. The yield statement returns a result that is determined by the Generator. The Scala For Comprehension has 3 expressions that better define the working of Scala For operation.
Generators, Filters, and Definitions.
scala>for{
| pattern <- ex //Generators
| pattern = ex // Definitions
| if ( pattern (any//conditionwhatever we want to define) //Filters
| } yield // The value it fetches.
The Generators are ones that will iterate over all the elements of a cod. The definition is something where we exactly write the thing we actually want to achieve or do. (like all the operations we want to perform can be put down into the definition part. And filters expressions are something that will drop the elements which will not follow a certain rule that is defined inside that. And finally, with the help of yield incomprehension, we can catch the result back whatever we want to.
Note: We can have comprehensions without yield also The only difference having that without yield is that the return type of comprehension will be changed to the unit.
Examples of Scala Comprehensions
Following are the examples are given below:
Example #1
Let us have a clear picture of that with an Example. We will try to fetch the name of a list from a particular add from a list.
scala> case class Details( name :String , Add : String)
defined class Details
scala> val d = List(Details("Name1","IND"),Details("Name2","USA"),Details("Name3","EN"))
d: List[Details] = List(Details(Name1,IND), Details(Name2,USA), Details(Name3,EN))
Now we will try to see with the help of the Scala Comprehension Code to fetch the name details from the particular Add.
scala> val e = for (a <- d if (a.Add =="IND" ))yield a.name
//The generators , Filters and Definition are defined here in the above logic where the function if has the filter
e: List[String] = List(Name1)
scala>e.foreach(name =>println(name))
Name1
Example #2
Let us see without the use of Yield. Even with the use of yield, we can make comprehensions but the return type then will be of type unit. Basically, we will try to understand what does the yield command does. While running the code a new empty collection of the same data type is made. It’s just like an empty bag of the same type of object where we can put our object.
Every iteration from the For loop creates a new output which is then put upon in the bag. And when the loop iteration is done the bag with the output item is returned. Comprehensions are just like a replacement to Map or flat map operations.
Let us look that with an example:
scala>val a = List(2,3,4,5,6)
a: List[Int] = List(2, 3, 4, 5, 6)
Here we see that the for statement with a yield on it produces the result.
scala>val b = for( d<- a ) yield d*2
b: List[Int] = List(4, 6, 8, 10, 12)
The same result can be seen if we use the Map function so scala comprehension produces the same value as the Map does.
scala>val b = a.map(_*2)
b: List[Int] = List(4, 6, 8, 10, 12)
Let us see how if works for FLATMAP, here we have defined a method and used flatMap over the list producing the same result throughout.
scala>deffn(v:Int) = List(v*2)
fn: (v: Int)List[Int]
scala>a.flatMap(x =>fn(x))
res0: List[Int] = List(4, 6, 8, 10, 12)
Example #3
We will see the above piece of code without yield:
scala> case class Details( name :String , Add : String)
defined class Details
scala> val d = List(Details("Name1","IND"),Details("Name2","USA"),Details("Name3","EN"))
d: List[Details] = List(Details(Name1,IND), Details(Name2,USA), Details(Name3,EN))
scala> val e = for (a <- d if (a.Add =="IND" ))println(a.name) // Without Yield Just printing the Name
Name1
e: Unit = ()
So we can deliberately use scala comprehension and make our object-oriented programming, better concise with a smaller piece of code having greater functionalities achieved.
Conclusion
So here from the above article we came to know across the various features and functioning of Scala Comprehensions. With the help of several examples, we saw how scala comprehension is used in the Scala Programming model and the various benefits of that. We came across how yield works and the various functionalities thereafter. So now we will be able to be write Code having Scala Comprehension over with that.
Recommended Articles
This is a guide to Scala Comprehensions. Here we also discuss the introduction and working of scala comprehensions along with examples and its code implementation. You may also have a look at the following articles to learn more –