Skip to content

Commit 69aa709

Browse files
Andrey FilyaninAndrey Filyanin
authored andcommitted
AF [SCALA-16] - For loops in Scala
1 parent 77877ac commit 69aa709

File tree

1 file changed

+5
-0
lines changed
  • core-scala/src/main/scala/com/baeldung/scala/forcomprehension

1 file changed

+5
-0
lines changed

core-scala/src/main/scala/com/baeldung/scala/forcomprehension/ForLoop.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ trait ForLoop {
5555
}
5656
}
5757

58+
// Iterating a Map of String to Strings
5859
def iterateMap (map: Map[String, String]): Unit = {
5960
for ((key,value) <- map) {
6061
sideEffectFunction(s"""$key is for $value""")
6162
}
6263
}
6364

65+
// Iterating a Map of String to List of Strings
6466
def iterateMapMultipleGenerators (deck: Map[String, List[String]]): Unit = {
6567
for {
6668
(suit, cards) <- deck
@@ -70,12 +72,14 @@ trait ForLoop {
7072
}
7173
}
7274

75+
// Pure List iteration
7376
def pureIteration (numbers: List[Int]): List[String] = {
7477
for (number <- numbers) yield {
7578
s"""$number + $number = ${number + number}"""
7679
}
7780
}
7881

82+
// Pure multiple Optional with for-comprehension
7983
def forComprehensionWithOptionals (someIntValue: Option[Int], someStringValue: Option[String]): Option[String] = {
8084
for {
8185
intValue <- someIntValue
@@ -85,6 +89,7 @@ trait ForLoop {
8589
}
8690
}
8791

92+
// Pure multiple Optional with map/flatMap
8893
def mapOptionals (someIntValue: Option[Int], someStringValue: Option[String]): Option[String] = {
8994
someIntValue.flatMap(intValue => someStringValue.map(stringValue => s"""$intValue is $stringValue"""))
9095
}

0 commit comments

Comments
 (0)