Skip to content

Commit d074cdd

Browse files
committed
BAEL-2041 Introduction To Scala
1 parent 28c33f8 commit d074cdd

File tree

9 files changed

+266
-0
lines changed

9 files changed

+266
-0
lines changed

core-scala/pom.xml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>core-scala</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-modules</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.scala-lang</groupId>
18+
<artifactId>scala-library</artifactId>
19+
<version>${scala.version}</version>
20+
</dependency>
21+
</dependencies>
22+
23+
<build>
24+
<sourceDirectory>src/main/scala</sourceDirectory>
25+
<testSourceDirectory>src/test/scala</testSourceDirectory>
26+
<plugins>
27+
<plugin>
28+
<groupId>net.alchim31.maven</groupId>
29+
<artifactId>scala-maven-plugin</artifactId>
30+
<version>3.3.2</version>
31+
<executions>
32+
<execution>
33+
<goals>
34+
<goal>compile</goal>
35+
<goal>testCompile</goal>
36+
</goals>
37+
<configuration>
38+
<args>
39+
<arg>-dependencyfile</arg>
40+
<arg>${project.build.directory}/.scala_dependencies</arg>
41+
</args>
42+
</configuration>
43+
</execution>
44+
</executions>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<properties>
50+
<scala.version>2.11.8</scala.version>
51+
</properties>
52+
</project>
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.scala
2+
3+
class Employee(val name: String, var salary: Int, annualIncrement: Int = 20) extends AnyRef {
4+
5+
def incrementSalary(): Unit = {
6+
salary += annualIncrement
7+
}
8+
9+
override def toString = s"Employee(name=$name, salary=$salary)"
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.scala
2+
3+
object HelloWorld extends App {
4+
println("Hello World!")
5+
args foreach println
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
package com.baeldung.scala
4+
5+
object HigherOrderFunctions {
6+
7+
def mapReduce(r: (Int, Int) => Int, i: Int, m: Int => Int, a: Int, b: Int) = {
8+
def iter(a: Int, result: Int): Int = {
9+
if (a > b) result
10+
else iter(a + 1, r(m(a), result))
11+
}
12+
iter(a, i)
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.scala
2+
3+
object Utils {
4+
def average(x: Double, y: Double) = (x + y) / 2
5+
6+
def gcd(x: Int, y: Int): Int = {
7+
if (y == 0) x else gcd(y, x % y)
8+
}
9+
10+
def gcdIter(x: Int, y: Int): Int = {
11+
var a = x
12+
var b = y
13+
while (b > 0) {
14+
a = a % b
15+
val t = a
16+
a = b
17+
b = t
18+
}
19+
return a
20+
}
21+
22+
def rangeSum(a: Int, b: Int) = {
23+
var sum = 0;
24+
for (i <- a to b) {
25+
sum += i
26+
}
27+
sum
28+
}
29+
30+
def factorial(a: Int): Int = {
31+
var result = 1;
32+
var i = a;
33+
do {
34+
result *= i
35+
i = i - 1
36+
} while (i > 0)
37+
result
38+
}
39+
40+
def randomLessThan(d: Double) = {
41+
var random = 0d
42+
do {
43+
random = Math.random()
44+
} while (random >= d)
45+
random
46+
}
47+
48+
def whileLoop(condition: => Boolean)(body: => Unit): Unit =
49+
if (condition) {
50+
body
51+
whileLoop(condition)(body)
52+
}
53+
54+
def power(x: Int, y: Int): Int = {
55+
def powNested(i: Int, accumulator: Int): Int = {
56+
if (i <= 0) accumulator
57+
else powNested(i - 1, x * accumulator)
58+
}
59+
powNested(y, 1)
60+
}
61+
62+
def fibonacci(n: Int): Int = n match {
63+
case 0 | 1 => 1
64+
case x if x > 1 => fibonacci(x - 1) + fibonacci(x - 2)
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.scala
2+
3+
import org.junit.Test
4+
import org.junit.Assert._
5+
6+
class EmployeeUnitTest {
7+
8+
@Test
9+
def whenEmployeeSalaryIncremented_thenCorrectSalary() = {
10+
val employee = new Employee("John Doe", 1000);
11+
employee.incrementSalary();
12+
assertEquals(1020, employee.salary);
13+
}
14+
15+
@Test
16+
def givenEmployee_whenToStringCalled_thenCorrectStringReturned() = {
17+
val employee = new Employee("John Doe", 1000);
18+
assertEquals("Employee(name=John Doe, salary=1000)", employee.toString);
19+
}
20+
}
21+
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.scala
2+
3+
import org.junit.Test
4+
import org.junit.Assert._
5+
import HigherOrderFunctions._
6+
7+
class HigherOrderFunctionsUnitTest {
8+
9+
@Test
10+
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned = {
11+
def square(x: Int) = x * x
12+
13+
def sum(x: Int, y: Int) = x + y
14+
15+
def sumSquares(a: Int, b: Int) =
16+
mapReduce(sum, 0, square, a, b)
17+
18+
val n = 10
19+
val expected = n * (n + 1) * (2 * n + 1) / 6
20+
assertEquals(expected, sumSquares(1, n));
21+
}
22+
23+
@Test
24+
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned = {
25+
def sumSquares(a: Int, b: Int) =
26+
mapReduce((x, y) => x + y, 0, x => x * x, a, b)
27+
28+
val n = 10
29+
val expected = n * (n + 1) * (2 * n + 1) / 6
30+
assertEquals(expected, sumSquares(1, n));
31+
}
32+
33+
@Test
34+
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned = {
35+
def sum(f: Int => Int)(a: Int, b: Int): Int =
36+
if (a > b) 0 else f(a) + sum(f)(a + 1, b)
37+
38+
def mod(n: Int)(x: Int) = x % n
39+
40+
def sumMod5 = sum(mod(5)) _
41+
42+
assertEquals(10, sumMod5(6,10));
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.scala
2+
3+
import org.junit.Test
4+
import Utils._
5+
import org.junit.Assert._
6+
7+
class UtilsUnitTest {
8+
9+
@Test
10+
def whenAverageCalled_thenCorrectValueReturned() = {
11+
val average = Utils.average(10, 20)
12+
assertEquals(15.0, average, 1e-5)
13+
}
14+
15+
@Test
16+
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned = {
17+
assertEquals(3, Utils.gcd(15, 27))
18+
}
19+
20+
@Test
21+
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned = {
22+
assertEquals(3, Utils.gcdIter(15, 27))
23+
}
24+
25+
@Test
26+
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned = {
27+
assertEquals(55, Utils.rangeSum(1, 10))
28+
}
29+
30+
@Test
31+
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned = {
32+
assertEquals(720, Utils.factorial(6))
33+
}
34+
35+
@Test
36+
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned = {
37+
val d = 0.1
38+
assertTrue(Utils.randomLessThan(d) < d)
39+
}
40+
41+
@Test
42+
def whenPowerInvokedWith2And3_then8Returned = {
43+
assertEquals(8, power(2, 3))
44+
}
45+
46+
@Test
47+
def whenFibonacciCalled_thenCorrectValueReturned = {
48+
assertEquals(34, fibonacci(8))
49+
}
50+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
<module>guava-modules/guava-21</module>
378378
<module>guice</module>
379379
<module>disruptor</module>
380+
<module>core-scala</module>
380381
<module>spring-static-resources</module>
381382
<module>hazelcast</module>
382383
<module>hbase</module>

0 commit comments

Comments
 (0)