Skip to content

Commit c1bca4d

Browse files
committed
Finish partial functions and function composition
1 parent ebbdcec commit c1bca4d

File tree

4 files changed

+92
-71
lines changed

4 files changed

+92
-71
lines changed

src/main/worksheets/f_Functions/d_partial_functions.sc

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/worksheets/f_Functions/e_function_composition.sc

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* A partial function in mathematics is only defined for a subset of the possible
3+
* values of its arguments.
4+
*
5+
* https://en.wikipedia.org/wiki/Partial_function
6+
*/
7+
def fraction(d: Int) = 42 / d
8+
9+
/**
10+
* Scala supports definition of partial functions by a special type [[PartialFunction]]
11+
*/
12+
object Routes extends PartialFunction[String, String] {
13+
override def isDefinedAt(route: String): Boolean = Seq("/hello", "/status") contains route
14+
15+
override def apply(route: String): String = route match {
16+
case "/hello" => "Hello World"
17+
case "/status" => "Alive"
18+
case _ => "404"
19+
}
20+
}
21+
22+
Routes("/hello")
23+
Routes.isDefinedAt("/status")
24+
Routes.isDefinedAt("/alive")
25+
26+
// Audience: What happens...
27+
// Routes("/alive")
28+
29+
30+
// syntactic sugar
31+
val dump: PartialFunction[Int, Int] = {
32+
case i if i % 2 == 0 => i + 1
33+
}
34+
35+
/**
36+
* Partial functions can be used everywhere a function is expected
37+
*/
38+
// Audience what would you expect?
39+
// 0 to 10 map dump
40+
41+
// Audience what would you expect?
42+
// 0 to 10 collect dump
43+
44+
45+
46+
47+
48+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Functions can be composed in several ways
3+
*/
4+
val times2: Int => Int = _ * 2
5+
val plus3: Int => Int = _ + 3
6+
7+
8+
(times2 andThen plus3)(3) == plus3(times2(3))
9+
10+
Function.chain(Seq(times2, plus3))(3) == plus3(times2(3))
11+
12+
(times2 compose plus3)(3) == times2(plus3(3))
13+
14+
/**
15+
* In addition, PartialFunctions can be composed via [[PartialFunction.orElse]]
16+
*/
17+
val okHandler: PartialFunction[Int, String] = {
18+
case 200 => "Do some cool stuff"
19+
}
20+
21+
val unauthorizedHandler: PartialFunction[Int, String] = {
22+
case 401 => "Login again"
23+
}
24+
25+
val paymentRequiredHandler: PartialFunction[Int, String] = {
26+
case 402 => "Pay the bills"
27+
}
28+
29+
val errorHandler: PartialFunction[Int, String] = {
30+
case 500 => "Server Error"
31+
}
32+
33+
val unknownHandler: PartialFunction[Int, String] = {
34+
case _ => "Just panic"
35+
}
36+
37+
def processResponse(status: Int, handler: Int => String): String = handler(status)
38+
39+
processResponse(200, okHandler orElse errorHandler)
40+
processResponse(500, okHandler orElse errorHandler)
41+
processResponse(5, okHandler orElse errorHandler orElse unknownHandler)
42+
43+
// Audience: What would you expect
44+
//processResponse(5, okHandler orElse errorHandler )

0 commit comments

Comments
 (0)