Skip to content

Commit b57a14d

Browse files
authored
Merge pull request eugenp#5416 from chandra1123/BAEL-2041
Bael-2041 Introduction to Scala
2 parents 331b274 + 464189f commit b57a14d

14 files changed

+400
-1
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ jmeter/src/main/resources/*-JMeter.csv
6363
**/tmp
6464
**/out-tsc
6565
**/nbproject/
66-
**/nb-configuration.xml
66+
**/nb-configuration.xml
67+
core-scala/.cache-main
68+
core-scala/.cache-tests

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.12.7</scala.version>
51+
</properties>
52+
</project>
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.scala
2+
3+
/**
4+
* Sample code demonstrating the various control structured.
5+
*
6+
* @author Chandra Prakash
7+
*
8+
*/
9+
object ControlStructuresDemo {
10+
def gcd(x : Int, y : Int) : Int = {
11+
if (y == 0) x else gcd(y, x % y)
12+
}
13+
14+
def gcdIter(x : Int, y : Int) : Int = {
15+
var a = x
16+
var b = y
17+
while (b > 0) {
18+
a = a % b
19+
val t = a
20+
a = b
21+
b = t
22+
}
23+
a
24+
}
25+
26+
def rangeSum(a : Int, b : Int) = {
27+
var sum = 0;
28+
for (i <- a to b) {
29+
sum += i
30+
}
31+
sum
32+
}
33+
34+
def factorial(a : Int) : Int = {
35+
var result = 1;
36+
var i = 1;
37+
do {
38+
result *= i
39+
i = i + 1
40+
} while (i <= a)
41+
result
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.scala
2+
3+
/**
4+
* Sample Code demonstrating a class.
5+
*
6+
* @author Chandra Prakash
7+
*
8+
*/
9+
class Employee(val name : String,
10+
var salary : Int,
11+
annualIncrement : Int = 20) {
12+
13+
def incrementSalary() : Unit = {
14+
salary += annualIncrement
15+
}
16+
17+
override def toString =
18+
s"Employee(name=$name, salary=$salary)"
19+
}
20+
21+
/**
22+
* A Trait which will make the toString return upper case value.
23+
*/
24+
trait UpperCasePrinter {
25+
override def toString = super.toString toUpperCase
26+
}
27+
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,27 @@
1+
package com.baeldung.scala
2+
3+
/**
4+
* Sample higher order functions.
5+
*
6+
* @author Chandra Prakash
7+
*
8+
*/
9+
object HigherOrderFunctions {
10+
11+
def mapReduce(r : (Int, Int) => Int,
12+
i : Int,
13+
m : Int => Int,
14+
a : Int, b : Int) = {
15+
def iter(a : Int, result : Int) : Int = {
16+
if (a > b) result
17+
else iter(a + 1, r(m(a), result))
18+
}
19+
iter(a, i)
20+
}
21+
22+
def whileLoop(condition : => Boolean)(body : => Unit) : Unit =
23+
if (condition) {
24+
body
25+
whileLoop(condition)(body)
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.scala
2+
3+
/**
4+
* An abstract class for set of integers and its implementation.
5+
*
6+
* @author Chandra Prakash
7+
*
8+
*/
9+
abstract class IntSet {
10+
// add an element to the set
11+
def incl(x : Int) : IntSet
12+
13+
// whether an element belongs to the set
14+
def contains(x : Int) : Boolean
15+
}
16+
17+
class EmptyIntSet extends IntSet {
18+
19+
def contains(x : Int) : Boolean = false
20+
21+
def incl(x : Int) =
22+
new NonEmptyIntSet(x, this)
23+
}
24+
25+
class NonEmptyIntSet(val head : Int, val tail : IntSet)
26+
extends IntSet {
27+
28+
def contains(x : Int) : Boolean =
29+
head == x || (tail contains x)
30+
31+
def incl(x : Int) : IntSet =
32+
if (this contains x) this
33+
else new NonEmptyIntSet(x, this)
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.scala
2+
3+
/**
4+
* Some utility methods.
5+
*
6+
* @author Chandra Prakash
7+
*
8+
*/
9+
object Utils {
10+
def average(x : Double, y : Double) = (x + y) / 2
11+
12+
def randomLessThan(d : Double) = {
13+
var random = 0d
14+
do {
15+
random = Math.random()
16+
} while (random >= d)
17+
random
18+
}
19+
20+
def power(x : Int, y : Int) : Int = {
21+
def powNested(i : Int, accumulator : Int) : Int = {
22+
if (i <= 0) accumulator
23+
else powNested(i - 1, x * accumulator)
24+
}
25+
powNested(y, 1)
26+
}
27+
28+
def fibonacci(n : Int) : Int = n match {
29+
case 0 | 1 => 1
30+
case x if x > 1 =>
31+
fibonacci(x - 1) + fibonacci(x - 2)
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.scala
2+
3+
import com.baeldung.scala.ControlStructuresDemo._
4+
import org.junit.Test
5+
import org.junit.Assert.assertEquals
6+
7+
class ControlStructuresDemoUnitTest {
8+
@Test
9+
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned = {
10+
assertEquals(3, gcd(15, 27))
11+
}
12+
13+
@Test
14+
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned = {
15+
assertEquals(3, gcdIter(15, 27))
16+
}
17+
18+
@Test
19+
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned = {
20+
assertEquals(55, rangeSum(1, 10))
21+
}
22+
23+
@Test
24+
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned = {
25+
assertEquals(720, factorial(6))
26+
}
27+
28+
@Test
29+
def whenFactorialOf0Invoked_then1Returned = {
30+
assertEquals(1, factorial(0))
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.scala
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
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+
@Test
22+
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned = {
23+
val employee =
24+
new Employee("John Doe", 1000) with UpperCasePrinter
25+
assertEquals("EMPLOYEE(NAME=JOHN DOE, SALARY=1000)", employee.toString)
26+
}
27+
28+
}
29+
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.scala
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
import HigherOrderFunctions.mapReduce
7+
8+
class HigherOrderFunctionsUnitTest {
9+
10+
@Test
11+
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned = {
12+
def square(x : Int) = x * x
13+
14+
def sum(x : Int, y : Int) = x + y
15+
16+
def sumSquares(a : Int, b : Int) =
17+
mapReduce(sum, 0, square, a, b)
18+
19+
assertEquals(385, sumSquares(1, 10))
20+
}
21+
22+
@Test
23+
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned = {
24+
def sumSquares(a : Int, b : Int) =
25+
mapReduce((x, y) => x + y, 0, x => x * x, a, b)
26+
27+
assertEquals(385, sumSquares(1, 10))
28+
}
29+
30+
@Test
31+
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned = {
32+
// a curried function
33+
def sum(f : Int => Int)(a : Int,
34+
b : Int) : Int =
35+
if (a > b) 0 else f(a) + sum(f)(a + 1, b)
36+
37+
// another curried function
38+
def mod(n : Int)(x : Int) = x % n
39+
40+
// application of a curried function
41+
assertEquals(1, mod(5)(6))
42+
43+
// partial application of curried function
44+
// trailing underscore is required to make function type explicit
45+
val sumMod5 = sum(mod(5)) _
46+
47+
assertEquals(10, sumMod5(6, 10))
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.scala
2+
3+
import scala.Range
4+
5+
import org.junit.Assert.assertFalse
6+
import org.junit.Test
7+
8+
class IntSetUnitTest {
9+
10+
@Test
11+
def givenSetof1To10_whenContains11Called_thenFalse = {
12+
13+
// Set up a set containing integers 1 to 10.
14+
val set1To10 =
15+
Range(1, 10)
16+
.foldLeft(new EmptyIntSet() : IntSet) {
17+
(x, y) => x incl y
18+
}
19+
20+
assertFalse(set1To10 contains 11)
21+
}
22+
23+
}

0 commit comments

Comments
 (0)