Skip to content

Commit 7fa4303

Browse files
cankillAndrey Filyanin
andauthored
SCALA-32 (#95)
Co-authored-by: Andrey Filyanin <[email protected]>
1 parent 86c99f9 commit 7fa4303

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

scala-core/src/main/scala/com/baeldung/scala/FunctionsAndMethods.scala renamed to scala-lang/src/main/scala/com/baeldung/scala/functionsandmethods/FunctionsAndMethods.scala

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package com.baeldung.scala
1+
package com.baeldung.scala.functionsandmethods
2+
3+
import scala.annotation.tailrec
24

35
object FunctionsAndMethods {
46
// Anonymous function execution
@@ -23,6 +25,27 @@ object FunctionsAndMethods {
2325

2426
// Here we defined a Value Class with extension method
2527
implicit class IntExtension(val value: Int) extends AnyVal { def isOdd: Boolean = value % 2 == 0 }
28+
29+
30+
def plot(f: Double => Double): List[Double] = {
31+
val xs: Range = -10 to 10
32+
xs.map(x => f(x)).toList
33+
}
34+
35+
val lines: (Double, Double, Double) => Double = (a,b,x) => a * x + b
36+
37+
val line: (Double, Double) => Double => Double = (a,b) => x => lines(a,b,x)
38+
39+
def factorial(num: Int): Int = {
40+
@tailrec
41+
def fact(num: Int, acc: Int): Int = {
42+
if (num == 0) acc else fact(num - 1, acc * num)
43+
}
44+
45+
fact(num, 1)
46+
}
47+
48+
def pop[T](seq: Seq[T]): T = seq.head
2649
}
2750

2851

scala-core/src/test/scala/com/baeldung/scala/FunctionsAndMethodsUnitTest.scala renamed to scala-lang/src/test/scala/com/baeldung/scala/functionsandmethods/FunctionsAndMethodsUnitTest.scala

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.scala
1+
package com.baeldung.scala.functionsandmethods
22

33
import org.junit.Assert.assertEquals
44
import org.junit.{Before, Test}
@@ -43,4 +43,33 @@ class FunctionsAndMethodsUnitTest {
4343
assertEquals(true, 10.isOdd)
4444
assertEquals(false, 11.isOdd)
4545
}
46+
47+
@Test
48+
def givenLine45_whenUseItInAPlot_thenCorrectResults(): Unit = {
49+
val a45DegreeLine = FunctionsAndMethods.line(1,0)
50+
val results = FunctionsAndMethods.plot(a45DegreeLine)
51+
val expected = List(-10.0, -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, -2.0, -1.0,
52+
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
53+
assertEquals(expected, results)
54+
}
55+
56+
@Test
57+
def givenNestedMethod_whenUseIt_thenCorrectResults(): Unit = {
58+
val factorialResult = FunctionsAndMethods.factorial(10)
59+
val expected = 3628800;
60+
assertEquals(expected, factorialResult)
61+
}
62+
63+
@Test
64+
def givenParameterizedMethod_whenUseIt_thenCorrectResults(): Unit = {
65+
val strings = Seq("a", "b", "c")
66+
val first = FunctionsAndMethods.pop(strings)
67+
assertEquals("a", first)
68+
69+
val ints = Seq(10, 3, 11, 22, 10)
70+
val second = FunctionsAndMethods.pop(ints)
71+
assertEquals(10, second)
72+
73+
74+
}
4675
}

0 commit comments

Comments
 (0)