-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-adt.worksheet.sc
45 lines (32 loc) · 1021 Bytes
/
2-adt.worksheet.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// <- Tools ************************************************
import scala.reflect.runtime.universe._
def prettyType[T: TypeTag](
v: T,
level: Int = 1
) = typeOf[T].baseClasses.map(typeOf[T].baseType).take(level).mkString(", ")
// ->
// <- ADT **************************************************
// redefining scala Boolean as an ADT
sealed trait Bool
case object True extends Bool
case object False extends Bool
case class Boolean2(
b1: Bool,
b2: Bool)
// ->
// <- Pattern Matching *************************************
// exhaustive match - compile will fail is a match is missing
def xor_1(boolean2: Boolean2) = boolean2 match {
case Boolean2(True, True) => 1
case Boolean2(False, False) => 1
case Boolean2(True, False) => 0
case Boolean2(False, True) => 0
}
xor_1(Boolean2(True, False))
// using if and _ placeholder
def xor_2(boolean2: Boolean2) = boolean2 match {
case Boolean2(b1, b2) if b1 == b2 => 1
case _ => 0
}
xor_2(Boolean2(True, False))
// ->