| 1. |
What Is Guard In Scala’s For-comprehension Construct? |
|
Answer» In Scala, for-comprehension construct has an if clause which is used to write a condition to filter some elements and generate NEW collection. This if clause is also known as “Guard”. If that guard is true, then add that ELEMENT to new collection. OTHERWISE, it does not add that element to original collection. Example:- For-comprehension Guard to generate only Even numbers into new collection. scala> val list = List(1,2,3,4,5,6,7,8,9,10) In Scala, for-comprehension construct has an if clause which is used to write a condition to filter some elements and generate new collection. This if clause is also known as “Guard”. If that guard is true, then add that element to new collection. Otherwise, it does not add that element to original collection. Example:- For-comprehension Guard to generate only Even numbers into new collection. scala> val list = List(1,2,3,4,5,6,7,8,9,10) |
|