Skip to content

Commit 6d1e164

Browse files
authored
Core scala refactor (eugenp#5654)
* encoding * Converting synchronous and asynchronous API to observables * Adding different ways of converting sync/async APIs to observalbles. * Replace anonymous class with lambda * update based on comment from Grzegorz Piwowarek * Refactor core-scala examples * Cleanup
1 parent 03f5301 commit 6d1e164

File tree

8 files changed

+24
-32
lines changed

8 files changed

+24
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ class Employee(val name : String,
2222
* A Trait which will make the toString return upper case value.
2323
*/
2424
trait UpperCasePrinter {
25-
override def toString = super.toString toUpperCase
25+
override def toString: String = super.toString toUpperCase
2626
}
2727

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object HigherOrderFunctions {
1111
def mapReduce(r : (Int, Int) => Int,
1212
i : Int,
1313
m : Int => Int,
14-
a : Int, b : Int) = {
14+
a : Int, b : Int): Int = {
1515
def iter(a : Int, result : Int) : Int = {
1616
if (a > b) result
1717
else iter(a + 1, r(m(a), result))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package com.baeldung.scala
77
*
88
*/
99
object Utils {
10-
def average(x : Double, y : Double) = (x + y) / 2
10+
def average(x : Double, y : Double): Double = (x + y) / 2
1111

12-
def randomLessThan(d : Double) = {
12+
def randomLessThan(d : Double): Double = {
1313
var random = 0d
1414
do {
1515
random = Math.random()

core-scala/src/test/scala/com/baeldung/scala/ControlStructuresDemoUnitTest.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
package com.baeldung.scala
22

33
import com.baeldung.scala.ControlStructuresDemo._
4-
import org.junit.Test
54
import org.junit.Assert.assertEquals
5+
import org.junit.Test
66

77
class ControlStructuresDemoUnitTest {
88
@Test
9-
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned = {
9+
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned() = {
1010
assertEquals(3, gcd(15, 27))
1111
}
1212

1313
@Test
14-
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned = {
14+
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned() = {
1515
assertEquals(3, gcdIter(15, 27))
1616
}
1717

1818
@Test
19-
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned = {
19+
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned() = {
2020
assertEquals(55, rangeSum(1, 10))
2121
}
2222

2323
@Test
24-
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned = {
24+
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned() = {
2525
assertEquals(720, factorial(6))
2626
}
2727

2828
@Test
29-
def whenFactorialOf0Invoked_then1Returned = {
29+
def whenFactorialOf0Invoked_then1Returned() = {
3030
assertEquals(1, factorial(0))
3131
}
3232

core-scala/src/test/scala/com/baeldung/scala/EmployeeUnitTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import org.junit.Test
66
class EmployeeUnitTest {
77

88
@Test
9-
def whenEmployeeSalaryIncremented_thenCorrectSalary = {
9+
def whenEmployeeSalaryIncremented_thenCorrectSalary() = {
1010
val employee = new Employee("John Doe", 1000)
1111
employee.incrementSalary()
1212
assertEquals(1020, employee.salary)
1313
}
1414

1515
@Test
16-
def givenEmployee_whenToStringCalled_thenCorrectStringReturned = {
16+
def givenEmployee_whenToStringCalled_thenCorrectStringReturned() = {
1717
val employee = new Employee("John Doe", 1000)
1818
assertEquals("Employee(name=John Doe, salary=1000)", employee.toString)
1919
}
2020

2121
@Test
22-
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned = {
22+
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned() = {
2323
val employee =
2424
new Employee("John Doe", 1000) with UpperCasePrinter
2525
assertEquals("EMPLOYEE(NAME=JOHN DOE, SALARY=1000)", employee.toString)

core-scala/src/test/scala/com/baeldung/scala/HigherOrderFunctionsUnitTest.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.baeldung.scala
22

3+
import com.baeldung.scala.HigherOrderFunctions.mapReduce
34
import org.junit.Assert.assertEquals
45
import org.junit.Test
56

6-
import HigherOrderFunctions.mapReduce
7-
87
class HigherOrderFunctionsUnitTest {
98

109
@Test
11-
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned = {
10+
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned() = {
1211
def square(x : Int) = x * x
1312

1413
def sum(x : Int, y : Int) = x + y
@@ -20,15 +19,15 @@ class HigherOrderFunctionsUnitTest {
2019
}
2120

2221
@Test
23-
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned = {
22+
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned() = {
2423
def sumSquares(a : Int, b : Int) =
2524
mapReduce((x, y) => x + y, 0, x => x * x, a, b)
2625

2726
assertEquals(385, sumSquares(1, 10))
2827
}
2928

3029
@Test
31-
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned = {
30+
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned() = {
3231
// a curried function
3332
def sum(f : Int => Int)(a : Int,
3433
b : Int) : Int =

core-scala/src/test/scala/com/baeldung/scala/IntSetUnitTest.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.baeldung.scala
22

3-
import scala.Range
4-
53
import org.junit.Assert.assertFalse
64
import org.junit.Test
75

86
class IntSetUnitTest {
97

108
@Test
11-
def givenSetof1To10_whenContains11Called_thenFalse = {
9+
def givenSetof1To10_whenContains11Called_thenFalse() = {
1210

1311
// Set up a set containing integers 1 to 10.
1412
val set1To10 =

core-scala/src/test/scala/com/baeldung/scala/UtilsUnitTest.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
package com.baeldung.scala
22

3-
import org.junit.Assert.assertEquals
4-
import org.junit.Assert.assertTrue
3+
import com.baeldung.scala.Utils.{average, fibonacci, power, randomLessThan}
4+
import org.junit.Assert.{assertEquals, assertTrue}
55
import org.junit.Test
66

7-
import Utils.average
8-
import Utils.fibonacci
9-
import Utils.power
10-
import Utils.randomLessThan
11-
127
class UtilsUnitTest {
138

149
@Test
15-
def whenAverageCalled_thenCorrectValueReturned() = {
10+
def whenAverageCalled_thenCorrectValueReturned(): Unit = {
1611
assertEquals(15.0, average(10, 20), 1e-5)
1712
}
1813

1914
@Test
20-
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned = {
15+
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned: Unit = {
2116
val d = 0.1
2217
assertTrue(randomLessThan(d) < d)
2318
}
2419

2520
@Test
26-
def whenPowerInvokedWith2And3_then8Returned = {
21+
def whenPowerInvokedWith2And3_then8Returned: Unit = {
2722
assertEquals(8, power(2, 3))
2823
}
2924

3025
@Test
31-
def whenFibonacciCalled_thenCorrectValueReturned = {
26+
def whenFibonacciCalled_thenCorrectValueReturned: Unit = {
3227
assertEquals(1, fibonacci(0))
3328
assertEquals(1, fibonacci(1))
3429
assertEquals(fibonacci(6),

0 commit comments

Comments
 (0)