File tree Expand file tree Collapse file tree 1 file changed +5
-0
lines changed
core-scala/src/main/scala/com/baeldung/scala/forcomprehension Expand file tree Collapse file tree 1 file changed +5
-0
lines changed Original file line number Diff line number Diff line change @@ -55,12 +55,14 @@ trait ForLoop {
55
55
}
56
56
}
57
57
58
+ // Iterating a Map of String to Strings
58
59
def iterateMap (map : Map [String , String ]): Unit = {
59
60
for ((key,value) <- map) {
60
61
sideEffectFunction(s """ $key is for $value""" )
61
62
}
62
63
}
63
64
65
+ // Iterating a Map of String to List of Strings
64
66
def iterateMapMultipleGenerators (deck : Map [String , List [String ]]): Unit = {
65
67
for {
66
68
(suit, cards) <- deck
@@ -70,12 +72,14 @@ trait ForLoop {
70
72
}
71
73
}
72
74
75
+ // Pure List iteration
73
76
def pureIteration (numbers : List [Int ]): List [String ] = {
74
77
for (number <- numbers) yield {
75
78
s """ $number + $number = ${number + number}"""
76
79
}
77
80
}
78
81
82
+ // Pure multiple Optional with for-comprehension
79
83
def forComprehensionWithOptionals (someIntValue : Option [Int ], someStringValue : Option [String ]): Option [String ] = {
80
84
for {
81
85
intValue <- someIntValue
@@ -85,6 +89,7 @@ trait ForLoop {
85
89
}
86
90
}
87
91
92
+ // Pure multiple Optional with map/flatMap
88
93
def mapOptionals (someIntValue : Option [Int ], someStringValue : Option [String ]): Option [String ] = {
89
94
someIntValue.flatMap(intValue => someStringValue.map(stringValue => s """ $intValue is $stringValue""" ))
90
95
}
You can’t perform that action at this time.
0 commit comments