Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
object PatternMatchCounter {
var count = 0
var isDark = false
def updateUI(): Unit = {
val theme = if(isDark) "Dark" else "Light"
count match {
case 0 => println(s"Counter is zero. Theme: $theme")
case n if n > 0 => println(s"Counter positive: $count. Theme: $theme")
case _ => println(s"Counter negative: $count. Theme: $theme")
}
}
def increment(): Unit = { count += 1; updateUI() }
def decrement(): Unit = { count -= 1; updateUI() }
def toggleTheme(): Unit = { isDark = !isDark; updateUI() }
def main(args: Array[String]): Unit = {
updateUI(); increment(); increment(); toggleTheme(); decrement()
}
}
PatternMatchCounter.main(Array())Coding works best on desktop or with an external keyboard.