Skip to content

Commit 77877ac

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

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.scala.forcomprehension
2+
3+
trait ForLoop {
4+
// We are defining a println in a separate function in order to be able to test our iterations
5+
def sideEffectFunction(arg: Any) = println(arg)
6+
7+
// Iterating an exclusive Range
8+
def iterateRangeTo (rangeTo: Range): Unit = {
9+
for (num <- rangeTo) {
10+
sideEffectFunction(num)
11+
}
12+
}
13+
14+
// Iterating an inclusive Range
15+
def iterateRangeUntil (rangeUntil: Range): Unit = {
16+
for (num <- rangeUntil) {
17+
sideEffectFunction(num)
18+
}
19+
}
20+
21+
// Iterating the Ranges as multiple generators
22+
def multipleGenerators (rangeTo: Range, rangeUntil: Range): Unit = {
23+
for {
24+
i <- rangeTo
25+
j <- rangeUntil
26+
} {
27+
sideEffectFunction(s"$i, $j")
28+
}
29+
}
30+
31+
// Iterating a Sequence of Strings
32+
def iterateCollection (colors: Seq[String]): Unit = {
33+
for (color <- colors) {
34+
sideEffectFunction(color)
35+
}
36+
}
37+
38+
// Iterating a Sequences of Strings, with multiple generators
39+
def iterateCollectionWithMultipleGenerators (colors: Seq[String]): Unit = {
40+
for (c1 <- colors; c2 <- colors; c3 <- colors) {
41+
sideEffectFunction(s"$c1$c2$c3 ")
42+
}
43+
}
44+
45+
// Iterating a Sequences of Strings, with multiple generators and guards
46+
def iterateCollectionsWithGuards(colors: Seq[String]): Unit = {
47+
for {
48+
c1 <- colors
49+
c2 <- colors
50+
if c2 != c1
51+
c3 <- colors
52+
if c3 != c2 && c3 != c1
53+
} {
54+
sideEffectFunction(s"$c1$c2$c3 ")
55+
}
56+
}
57+
58+
def iterateMap (map: Map[String, String]): Unit = {
59+
for ((key,value) <- map) {
60+
sideEffectFunction(s"""$key is for $value""")
61+
}
62+
}
63+
64+
def iterateMapMultipleGenerators (deck: Map[String, List[String]]): Unit = {
65+
for {
66+
(suit, cards) <- deck
67+
card <- cards
68+
} {
69+
sideEffectFunction(s"""$card of $suit""")
70+
}
71+
}
72+
73+
def pureIteration (numbers: List[Int]): List[String] = {
74+
for (number <- numbers) yield {
75+
s"""$number + $number = ${number + number}"""
76+
}
77+
}
78+
79+
def forComprehensionWithOptionals (someIntValue: Option[Int], someStringValue: Option[String]): Option[String] = {
80+
for {
81+
intValue <- someIntValue
82+
stringValue <- someStringValue
83+
} yield {
84+
s"""$intValue is $stringValue"""
85+
}
86+
}
87+
88+
def mapOptionals (someIntValue: Option[Int], someStringValue: Option[String]): Option[String] = {
89+
someIntValue.flatMap(intValue => someStringValue.map(stringValue => s"""$intValue is $stringValue"""))
90+
}
91+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.baeldung.scala.forcomprehension
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.{Before, Test}
5+
6+
class ForLoopImplementation extends ForLoop {
7+
var sideEffectResult: Seq[Any] = Nil
8+
override def sideEffectFunction (arg: Any): Unit = {
9+
sideEffectResult = sideEffectResult :+ arg
10+
}
11+
12+
def clean(): Unit = {
13+
sideEffectResult = Nil;
14+
}
15+
}
16+
17+
class ForLoopTest {
18+
val forLoopTest = new ForLoopImplementation
19+
val rangeTo: Range = 1 to 3
20+
val rangeUntil: Range = 1 until 3
21+
val colors = Seq("R", "G", "B")
22+
val map = Map("R" -> "Red", "G" -> "Green", "B" -> "Blue")
23+
val deck = Map("" -> List("A", "K", "Q"),
24+
"" -> List("J", "10"),
25+
"" -> List("9", "8", "7"),
26+
"" -> List("A", "K", "J", "6"))
27+
val numbers = List(1, 2, 3)
28+
val someIntValue: Option[Int] = Some(10)
29+
val someStringValue: Option[String] = Some("Ten")
30+
31+
@Before
32+
def init(): Unit = {
33+
forLoopTest.clean()
34+
}
35+
36+
@Test
37+
def iterateRangeToTest (): Unit = {
38+
forLoopTest.iterateRangeTo(rangeTo)
39+
val expected = Seq(1, 2, 3)
40+
assertEquals(expected, forLoopTest.sideEffectResult)
41+
}
42+
43+
@Test
44+
def iterateRangeUntilTest (): Unit = {
45+
forLoopTest.iterateRangeUntil(rangeUntil)
46+
val expected = Seq(1, 2)
47+
assertEquals(expected, forLoopTest.sideEffectResult)
48+
}
49+
50+
@Test
51+
def multipleGeneratorsTest (): Unit = {
52+
forLoopTest.multipleGenerators(rangeTo, rangeUntil)
53+
val expected = Seq("1, 1", "1, 2", "2, 1", "2, 2", "3, 1", "3, 2")
54+
assertEquals(expected, forLoopTest.sideEffectResult)
55+
}
56+
57+
@Test
58+
def iterateCollectionTest (): Unit = {
59+
forLoopTest.iterateCollection(colors)
60+
val expected = Seq("R", "G", "B")
61+
assertEquals(expected, forLoopTest.sideEffectResult)
62+
}
63+
64+
@Test
65+
def iterateCollectionWithMultipleGeneratorsTest (): Unit = {
66+
forLoopTest.iterateCollectionWithMultipleGenerators(colors)
67+
val expected = List("RRR ", "RRG ", "RRB ", "RGR ", "RGG ", "RGB ", "RBR ", "RBG ", "RBB ", "GRR ", "GRG ", "GRB ", "GGR ", "GGG ", "GGB ", "GBR ", "GBG ", "GBB ", "BRR ", "BRG ", "BRB ", "BGR ", "BGG ", "BGB ", "BBR ", "BBG ", "BBB ")
68+
assertEquals(expected, forLoopTest.sideEffectResult)
69+
}
70+
71+
@Test
72+
def iterateCollectionsWithGuardsTest (): Unit = {
73+
forLoopTest.iterateCollectionsWithGuards(colors)
74+
val expected = List("RGB ", "RBG ", "GRB ", "GBR ", "BRG ", "BGR ")
75+
assertEquals(expected, forLoopTest.sideEffectResult)
76+
}
77+
78+
@Test
79+
def iterateMapTest (): Unit = {
80+
forLoopTest.iterateMap(map)
81+
val expected = List("R is for Red", "G is for Green", "B is for Blue")
82+
assertEquals(expected, forLoopTest.sideEffectResult)
83+
}
84+
85+
@Test
86+
def iterateMapMultipleGeneratorsTest (): Unit = {
87+
forLoopTest.iterateMapMultipleGenerators(deck)
88+
val expected = List("A of ♣", "K of ♣", "Q of ♣", "J of ♦", "10 of ♦", "9 of ♥", "8 of ♥", "7 of ♥", "A of ♠", "K of ♠", "J of ♠", "6 of ♠")
89+
assertEquals(expected, forLoopTest.sideEffectResult)
90+
}
91+
92+
@Test
93+
def pureIterationTest (): Unit = {
94+
val result = forLoopTest.pureIteration(numbers)
95+
val expected = List("1 + 1 = 2", "2 + 2 = 4", "3 + 3 = 6")
96+
assertEquals(expected, result)
97+
}
98+
99+
@Test
100+
def forComprehensionWithOptionalsTest (): Unit = {
101+
102+
val resultFor = forLoopTest.forComprehensionWithOptionals(someIntValue, someStringValue)
103+
val resultMap = forLoopTest.mapOptionals(someIntValue, someStringValue)
104+
val expected = Some("10 is Ten")
105+
assertEquals(expected, resultFor)
106+
assertEquals(expected, resultMap)
107+
assertEquals(resultFor, resultMap)
108+
}
109+
}

0 commit comments

Comments
 (0)