Core scala refactor (#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
This commit is contained in:
Grzegorz Piwowarek 2018-11-10 12:45:08 +01:00 committed by GitHub
parent 03f530126f
commit 6d1e1647d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 32 deletions

View File

@ -22,6 +22,6 @@ class Employee(val name : String,
* A Trait which will make the toString return upper case value.
*/
trait UpperCasePrinter {
override def toString = super.toString toUpperCase
override def toString: String = super.toString toUpperCase
}

View File

@ -11,7 +11,7 @@ object HigherOrderFunctions {
def mapReduce(r : (Int, Int) => Int,
i : Int,
m : Int => Int,
a : Int, b : Int) = {
a : Int, b : Int): Int = {
def iter(a : Int, result : Int) : Int = {
if (a > b) result
else iter(a + 1, r(m(a), result))

View File

@ -7,9 +7,9 @@ package com.baeldung.scala
*
*/
object Utils {
def average(x : Double, y : Double) = (x + y) / 2
def average(x : Double, y : Double): Double = (x + y) / 2
def randomLessThan(d : Double) = {
def randomLessThan(d : Double): Double = {
var random = 0d
do {
random = Math.random()

View File

@ -1,32 +1,32 @@
package com.baeldung.scala
import com.baeldung.scala.ControlStructuresDemo._
import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.Test
class ControlStructuresDemoUnitTest {
@Test
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned = {
def givenTwoIntegers_whenGcdCalled_thenCorrectValueReturned() = {
assertEquals(3, gcd(15, 27))
}
@Test
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned = {
def givenTwoIntegers_whenGcdIterCalled_thenCorrectValueReturned() = {
assertEquals(3, gcdIter(15, 27))
}
@Test
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned = {
def givenTwoIntegers_whenRangeSumcalled_thenCorrectValueReturned() = {
assertEquals(55, rangeSum(1, 10))
}
@Test
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned = {
def givenPositiveInteger_whenFactorialInvoked_thenCorrectValueReturned() = {
assertEquals(720, factorial(6))
}
@Test
def whenFactorialOf0Invoked_then1Returned = {
def whenFactorialOf0Invoked_then1Returned() = {
assertEquals(1, factorial(0))
}

View File

@ -6,20 +6,20 @@ import org.junit.Test
class EmployeeUnitTest {
@Test
def whenEmployeeSalaryIncremented_thenCorrectSalary = {
def whenEmployeeSalaryIncremented_thenCorrectSalary() = {
val employee = new Employee("John Doe", 1000)
employee.incrementSalary()
assertEquals(1020, employee.salary)
}
@Test
def givenEmployee_whenToStringCalled_thenCorrectStringReturned = {
def givenEmployee_whenToStringCalled_thenCorrectStringReturned() = {
val employee = new Employee("John Doe", 1000)
assertEquals("Employee(name=John Doe, salary=1000)", employee.toString)
}
@Test
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned = {
def givenEmployeeWithTrait_whenToStringCalled_thenCorrectStringReturned() = {
val employee =
new Employee("John Doe", 1000) with UpperCasePrinter
assertEquals("EMPLOYEE(NAME=JOHN DOE, SALARY=1000)", employee.toString)

View File

@ -1,14 +1,13 @@
package com.baeldung.scala
import com.baeldung.scala.HigherOrderFunctions.mapReduce
import org.junit.Assert.assertEquals
import org.junit.Test
import HigherOrderFunctions.mapReduce
class HigherOrderFunctionsUnitTest {
@Test
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned = {
def whenCalledWithSumAndSquareFunctions_thenCorrectValueReturned() = {
def square(x : Int) = x * x
def sum(x : Int, y : Int) = x + y
@ -20,7 +19,7 @@ class HigherOrderFunctionsUnitTest {
}
@Test
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned = {
def whenComputingSumOfSquaresWithAnonymousFunctions_thenCorrectValueReturned() = {
def sumSquares(a : Int, b : Int) =
mapReduce((x, y) => x + y, 0, x => x * x, a, b)
@ -28,7 +27,7 @@ class HigherOrderFunctionsUnitTest {
}
@Test
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned = {
def givenCurriedFunctions_whenInvoked_thenCorrectValueReturned() = {
// a curried function
def sum(f : Int => Int)(a : Int,
b : Int) : Int =

View File

@ -1,14 +1,12 @@
package com.baeldung.scala
import scala.Range
import org.junit.Assert.assertFalse
import org.junit.Test
class IntSetUnitTest {
@Test
def givenSetof1To10_whenContains11Called_thenFalse = {
def givenSetof1To10_whenContains11Called_thenFalse() = {
// Set up a set containing integers 1 to 10.
val set1To10 =

View File

@ -1,34 +1,29 @@
package com.baeldung.scala
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import com.baeldung.scala.Utils.{average, fibonacci, power, randomLessThan}
import org.junit.Assert.{assertEquals, assertTrue}
import org.junit.Test
import Utils.average
import Utils.fibonacci
import Utils.power
import Utils.randomLessThan
class UtilsUnitTest {
@Test
def whenAverageCalled_thenCorrectValueReturned() = {
def whenAverageCalled_thenCorrectValueReturned(): Unit = {
assertEquals(15.0, average(10, 20), 1e-5)
}
@Test
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned = {
def whenRandomLessThanInvokedWithANumber_thenARandomLessThanItReturned: Unit = {
val d = 0.1
assertTrue(randomLessThan(d) < d)
}
@Test
def whenPowerInvokedWith2And3_then8Returned = {
def whenPowerInvokedWith2And3_then8Returned: Unit = {
assertEquals(8, power(2, 3))
}
@Test
def whenFibonacciCalled_thenCorrectValueReturned = {
def whenFibonacciCalled_thenCorrectValueReturned: Unit = {
assertEquals(1, fibonacci(0))
assertEquals(1, fibonacci(1))
assertEquals(fibonacci(6),