Code · 1 file
Sample Scala File
Scala 3 combines functional and object-oriented programming on the JVM. The Scala sample is a small program — a customer record, a VIP check and a formatted total — that is the same in every language we offer, so you can compare them side by side.
Scala · 519 B
Sample Scala file
An idiomatic Scala example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.
CC0 · free for any use · Verified by an independent tool · Report a problem
// Sample Scala 3 program — samplefiles.com
case class Customer(id: Int, name: String, balance: Double):
def isVip: Boolean = balance >= 10_000
def totalBalance(customers: Seq[Customer]): Double = customers.map(_.balance).sum
@main def run(): Unit =
val customers = Seq(Customer(1, "Ada", 12_500), Customer(2, "Grace", 830.5), Customer(3, "Linus", 10_000))
for c <- customers do println(f"${c.name}%-6s ${c.balance}%10.2f ${if c.isVip then "VIP" else ""}")
println(f"Total: ${totalBalance(customers)}%.2f")File details
Download from the command line
curl -L -o Sample.scala https://files.samplefiles.com/code/scala/Sample.scalawget https://files.samplefiles.com/code/scala/Sample.scalahttps://files.samplefiles.com/code/scala/Sample.scalaprintf '%s %s\n' 01d78fb4f1f57f8baa3d3864da77c8ed518482a9e3b0f898cce29fb757c2233e Sample.scala | shasum -a 256 -cOr check the download in your browser with the checksum calculator.