1. Home
  2. /
  3. Scala
  4. /
  5. Counter with History

Counter with History - Scala Typing CST Test

Loading…

Counter with History — Scala Code

A console-based Scala counter that logs each increment/decrement action in a history list.

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())

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.

More Scala Typing Exercises

Scala Theme Toggle and CounterScala Counter with Conditional ThemeScala Counter Using FunctionsScala Counter with For Loop SimulationScala Counter with Pattern MatchingScala Counter with Higher-Order FunctionsScala Counter with Tail RecursionScala Counter with Option TypeScala Counter with Map and Fold

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher