Skip to content

Commit a1f3b2e

Browse files
committed
Merge branch 'master' of github.com:Baeldung/scala-tutorials into feature/SCALA-46
2 parents 71d7b79 + 5bb2ae6 commit a1f3b2e

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

core-scala/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ This module contains articles about Scala's core features
77
- [Introduction to Scala](https://www.baeldung.com/scala-intro)
88
- [Regular Expressions in Scala](https://www.baeldung.com/scala/regular-expressions)
99
- [Higher-Order Functions in Scala](https://www.baeldung.com/scala/higher-order-functions)
10-
10+
- [A Guide to Scala Tuples](https://www.baeldung.com/scala/tuples)
11+
- [Tail Recursion in Scala](https://www.baeldung.com/scala/tail-recursion)
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)