Learn SCALA with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
Scala Theme Toggle and Counter
object Main {
var count: Int = 0
var isDark: Boolean = false
def updateUI(): Unit = {
println(s"Counter: $count")
println(s"Theme: ${if(isDark) "Dark" else "Light"}")
}
def increment(): Unit = { count += 1; updateUI() }
def decrement(): Unit = { count -= 1; updateUI() }
def reset(): Unit = { count = 0; updateUI() }
def toggleTheme(): Unit = { isDark = !isDark; updateUI() }
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()
}
Main.main(Array())
Demonstrates a simple counter with theme toggling using Scala, runnable in a console or JVM environment.
Scala Counter with History
object CounterWithHistory {
var count = 0
var history = List[String]()
def updateUI(): Unit = {
println(s"Counter: $count")
println(s"History: ${history.mkString(", ")}")
}
def increment(): Unit = { count += 1; history = history :+ s"Incremented to $count"; updateUI() }
def decrement(): Unit = { count -= 1; history = history :+ s"Decremented to $count"; updateUI() }
def reset(): Unit = { count = 0; history = List("Counter reset"); updateUI() }
def main(args: Array[String]): Unit = {
updateUI()
increment()
increment()
decrement()
reset()
}
}
CounterWithHistory.main(Array())
A console-based Scala counter that logs each increment/decrement action in a history list.
Scala Counter with Conditional Theme
object ConditionalThemeCounter {
var count = 0
var isDark = false
def updateUI(): Unit = {
isDark = count % 2 == 0
println(s"Counter: $count")
println(s"Theme: ${if(isDark) "Dark" else "Light"}")
}
def increment(): Unit = { count += 1; updateUI() }
def decrement(): Unit = { count -= 1; updateUI() }
def reset(): Unit = { count = 0; updateUI() }
def main(args: Array[String]): Unit = {
updateUI()
increment()
increment()
increment()
decrement()
reset()
}
}
ConditionalThemeCounter.main(Array())
Counter in Scala that switches theme based on value thresholds.
Scala Counter Using Functions
object FunctionCounter {
var count = 0
var isDark = false
val updateUI = () => println(s"Counter: $count, Theme: ${if(isDark) "Dark" else "Light"}")
val increment = () => { count += 1; updateUI() }
val decrement = () => { count -= 1; updateUI() }
val reset = () => { count = 0; updateUI() }
val toggleTheme = () => { isDark = !isDark; updateUI() }
def main(args: Array[String]): Unit = {
updateUI()
increment()
toggleTheme()
decrement()
reset()
}
}
FunctionCounter.main(Array())
Demonstrates using Scala functions for counter operations and theme toggling.
Scala Counter with For Loop Simulation
object LoopCounter {
var count = 0
var isDark = false
def updateUI(): Unit = println(s"Counter: $count, Theme: ${if(isDark) "Dark" else "Light"}")
def main(args: Array[String]): Unit = {
for(i <- 1 to 3) { count += 1; updateUI() }
isDark = !isDark; updateUI()
for(i <- 1 to 2) { count -= 1; updateUI() }
count = 0; updateUI()
}
}
LoopCounter.main(Array())
Scala counter that simulates multiple increments and decrements using a loop.
Scala Counter with Pattern Matching
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())
Demonstrates Scala pattern matching to display custom messages for counter values.
Scala Counter with Higher-Order Functions
object HigherOrderCounter {
var count = 0
var isDark = false
def updateUI(): Unit = println(s"Counter: $count, Theme: ${if(isDark) "Dark" else "Light"}")
def perform(action: () => Unit) = action()
def main(args: Array[String]): Unit = {
perform(() => { count += 1; updateUI() })
perform(() => { count += 1; updateUI() })
perform(() => { isDark = !isDark; updateUI() })
perform(() => { count -= 1; updateUI() })
}
}
HigherOrderCounter.main(Array())
Demonstrates Scala higher-order functions for counter operations and theme toggling.
Scala Counter with Tail Recursion
object TailRecursiveCounter {
var count = 0
var isDark = false
def updateUI(): Unit = println(s"Counter: $count, Theme: ${if(isDark) "Dark" else "Light"}")
@annotation.tailrec
def simulate(steps: List[String]): Unit = steps match {
case Nil => updateUI()
case head :: tail => head match {
case "inc" => count += 1
case "dec" => count -= 1
case "toggle" => isDark = !isDark
}; updateUI(); simulate(tail)
}
def main(args: Array[String]): Unit = simulate(List("inc","inc","toggle","dec","reset"))
}
TailRecursiveCounter.main(Array())
Demonstrates using tail recursion to simulate multiple counter operations in Scala.
Scala Counter with Option Type
object OptionCounter {
var count = 0
var isDark = false
def updateUI(): Unit = println(s"Counter: $count, Theme: ${if(isDark) "Dark" else "Light"}")
def reset(opt: Option[Boolean]): Unit = opt.foreach(_ => { count = 0; updateUI() })
def main(args: Array[String]): Unit = {
updateUI(); count += 2; updateUI(); reset(Some(true));
}
}
OptionCounter.main(Array())
Demonstrates using Scala Option type to optionally reset the counter.
Scala Counter with Map and Fold
object MapFoldCounter {
var count = 0
var isDark = false
def updateUI(): Unit = println(s"Counter: $count, Theme: ${if(isDark) "Dark" else "Light"}")
def main(args: Array[String]): Unit = {
val actions = List("inc", "inc", "toggle", "dec", "reset")
actions.foldLeft(()) { (_, action) =>
action match {
case "inc" => count +=1
case "dec" => count -=1
case "toggle" => isDark = !isDark
case "reset" => count = 0
}; updateUI()
}
}
}
MapFoldCounter.main(Array())
Uses Scala collections Map and fold to simulate a sequence of counter operations.