Skip to content

Commit 5bb2ae6

Browse files
authored
Merge pull request #30 from barbasa/master
SCALA-45 Partial Functions
2 parents 94bd877 + 3d9f1ee commit 5bb2ae6

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.scala.partialfunctions
2+
3+
object SquareRoot {
4+
val squareRoot = new PartialFunction[Double, Double] {
5+
def apply(x: Double) = Math.sqrt(x)
6+
def isDefinedAt(x: Double) = x >= 0
7+
}
8+
9+
val squareRootImplicit: PartialFunction[Double, Double] = {
10+
case x if x >= 0 => Math.sqrt(x)
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.scala.partialfunctions
2+
3+
object SwapIntegerSign {
4+
5+
private val negativeOrZeroToPositive: PartialFunction[Int, Int] = {
6+
case x if x <= 0 => Math.abs(x)
7+
}
8+
9+
private val positiveToNegative: PartialFunction[Int, Int] = {
10+
case x if x > 0 => -1 * x
11+
}
12+
13+
val swapSign: PartialFunction[Int, Int] = {
14+
positiveToNegative orElse negativeOrZeroToPositive
15+
}
16+
17+
val printIfPositive: PartialFunction[Int, Unit] = {
18+
case x if x > 0 => println(s"$x is positive!")
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.scala.partialfunctions
2+
3+
import org.scalatest.FlatSpec
4+
5+
class SquareRootUnitTest extends FlatSpec {
6+
7+
"SquareRoot" should "return the correct value for positive double" in {
8+
assert(SquareRoot.squareRoot(4.0) == 2.0)
9+
}
10+
11+
it should "return NaN for a negative double" in {
12+
assert(SquareRoot.squareRoot(-4.0).equals(Double.NaN))
13+
}
14+
15+
"SquareRootImplicit" should "return the correct value for a positive double" in {
16+
assert(SquareRoot.squareRootImplicit(4.0) == 2.0)
17+
}
18+
19+
it should "throw a 'scala.MatchError' exception" in {
20+
assertThrows[scala.MatchError] {
21+
SquareRoot.squareRootImplicit(-4.0)
22+
}
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.scala.partialfunctions
2+
3+
import org.scalatest.FlatSpec
4+
5+
class SwapIntegerSignUnitTest extends FlatSpec {
6+
7+
"swapSign" should "return a positive number when a negative is given" in {
8+
assert(SwapIntegerSign.swapSign(-3) == 3)
9+
}
10+
11+
it should "return a negative number when a positive is given" in {
12+
assert(SwapIntegerSign.swapSign(3) == -3)
13+
}
14+
15+
"printIfPositive" should "print a positive number" in {
16+
assert(SwapIntegerSign.printIfPositive(3).getClass == classOf[Unit])
17+
}
18+
19+
it should "throw a 'scala.MatchError' exception given a negative number" in {
20+
assertThrows[scala.MatchError] {
21+
SwapIntegerSign.printIfPositive(-3)
22+
}
23+
}
24+
25+
"swapSign andThen printIfPositive" should "chain and print a positive number" in {
26+
assert(
27+
(SwapIntegerSign.swapSign andThen SwapIntegerSign.printIfPositive)(-1).getClass == classOf[
28+
Unit
29+
]
30+
)
31+
}
32+
33+
it should "chain and throw a 'scala.MatchError' exception given a negative number" in {
34+
assertThrows[scala.MatchError] {
35+
(SwapIntegerSign.swapSign andThen SwapIntegerSign.printIfPositive)(1)
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)