Skip to content

Commit 464189f

Browse files
committed
Introduction to Scala
Updated examples
1 parent d074cdd commit 464189f

13 files changed

+297
-164
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</build>
4848

4949
<properties>
50-
<scala.version>2.11.8</scala.version>
50+
<scala.version>2.12.7</scala.version>
5151
</properties>
5252
</project>
5353

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+
}

core-scala/src/main/scala/com/baeldung/scala/Employee.scala

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
package com.baeldung.scala
22

3-
class Employee(val name: String, var salary: Int, annualIncrement: Int = 20) extends AnyRef {
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) {
412

5-
def incrementSalary(): Unit = {
6-
salary += annualIncrement
7-
}
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+
}
827

9-
override def toString = s"Employee(name=$name, salary=$salary)"
10-
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.baeldung.scala
22

33
object HelloWorld extends App {
4-
println("Hello World!")
5-
args foreach println
4+
println("Hello World!")
5+
args foreach println
66
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
2-
31
package com.baeldung.scala
42

3+
/**
4+
* Sample higher order functions.
5+
*
6+
* @author Chandra Prakash
7+
*
8+
*/
59
object HigherOrderFunctions {
610

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)
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)
1326
}
1427
}
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
@@ -1,66 +1,33 @@
11
package com.baeldung.scala
22

3+
/**
4+
* Some utility methods.
5+
*
6+
* @author Chandra Prakash
7+
*
8+
*/
39
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)
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)
6524
}
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+
}
6633
}
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
@@ -1,22 +1,30 @@
11
package com.baeldung.scala
22

3+
import org.junit.Assert.assertEquals
34
import org.junit.Test
4-
import org.junit.Assert._
55

66
class EmployeeUnitTest {
77

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-
}
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+
2028
}
2129

2230

0 commit comments

Comments
 (0)