Counter with Pattern Matching - Scala Typing CST Test
Loading…
Counter with Pattern Matching — Scala Code
Demonstrates Scala pattern matching to display custom messages for counter values.
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())Scala Language Guide
Scala is a high-level, general-purpose programming language that combines object-oriented and functional programming paradigms. It is designed to be concise, expressive, and interoperable with Java, running on the Java Virtual Machine (JVM).
Primary Use Cases
- ▸Backend development (e.g., with Play Framework or Akka)
- ▸Big data processing (Apache Spark, Kafka)
- ▸Functional programming projects
- ▸Microservices and distributed systems
- ▸DSLs and highly expressive codebases
Notable Features
- ▸Statically typed with type inference
- ▸Supports both object-oriented and functional programming
- ▸Seamless Java interoperability
- ▸Pattern matching and algebraic data types
- ▸Concurrent programming support via Akka actors
Origin & Creator
Created in 2003 by Martin Odersky at EPFL (École Polytechnique Fédérale de Lausanne), Switzerland.
Industrial Note
Scala is widely used in big data, distributed systems, and backend services where JVM interoperability and functional programming benefits are critical.