Scala Cheat Sheet
Scala syntax, case classes, pattern matching, immutable collections, and functional programming features that run on the JVM.
2 PagesAdvancedApr 15, 2026
Basic Syntax
Values, variables, and control flow.
scala
val age = 30 // immutable valuevar count = 0 // mutable variableval name = "Ada"val pi: Double = 3.14159if (age >= 18) { println(s"$name is an adult")}for (i <- 0 until 5) { println(s"Count: $i")}
Case Classes & Pattern Matching
Idiomatic data modeling and dispatch.
scala
case class Point(x: Int, y: Int)val p = Point(1, 2)val p2 = p.copy(y = 5) // immutable updatedef describe(x: Any): String = x match { case 0 => "zero" case n: Int if n > 0 => "positive" case Point(0, 0) => "origin" case Point(_, _) => "some point" case _ => "unknown"}
Collections
Immutable, functional collection operations.
scala
val nums = List(5, 3, 1, 4, 2)val evens = nums.filter(_ % 2 == 0) // List(4, 2)val squared = nums.map(n => n * n) // List(25, 9, 1, 16, 4)val sum = nums.sum // 15val sorted = nums.sorted // List(1, 2, 3, 4, 5)val opt: Option[Int] = nums.find(_ > 10) // None
Core Keywords
Common Scala language keywords.
- val/var- immutable and mutable bindings (prefer val)
- case class- immutable data class with structural equality and copy()
- object- defines a singleton (used for companion objects, static-like members)
- trait- reusable interface that can hold implementation, mixed in with 'with'
- Option[T]- Some(value) or None instead of null
- match- powerful pattern-matching expression
- for (a <- xs) yield- for-comprehension producing a new collection
- implicit- compiler-provided parameter or conversion (use sparingly)
For-Comprehensions
Desugared map/flatMap/withFilter chains for monadic composition.
scala
val xs = List(1, 2, 3)val ys = List(10, 20)// Cartesian pairs with a guardval pairs = for { x <- xs y <- ys if x % 2 == 1} yield (x, y)// List((1,10),(1,20),(3,10),(3,20))// Works for any monad, e.g. Option / Either / Futureval total = for { a <- Some(3) b <- Some(4)} yield a + b // Some(7)
Given / Using (Scala 3 Contextual Abstractions)
Type classes via given instances and using clauses.
scala
trait Show[A]: def show(a: A): Stringgiven Show[Int] with def show(a: Int): String = s"Int($a)"def print[A](a: A)(using s: Show[A]): String = s.show(a)print(42) // "Int(42)"// Extension methodsextension (x: Int) def squared: Int = x * x5.squared // 25
Futures & Concurrency
Asynchronous computation with Future and ExecutionContext.
scala
import scala.concurrent.{Future, Await}import scala.concurrent.ExecutionContext.Implicits.globalimport scala.concurrent.duration._val f = Future { Thread.sleep(100); 21 * 2 }f.map(_ + 1).foreach(println) // 43 (async)f.recover { case _: Exception => 0 }val combined = for { a <- Future(1) b <- Future(2)} yield a + bAwait.result(combined, 2.seconds) // 3
Higher-Order Collection Operations
Common transformations beyond map/filter.
- foldLeft(z)(f)- accumulate left-to-right with a seed value
- groupBy(f)- build a Map from a key function to sublists
- partition(p)- split into (matching, non-matching) tuple
- collect(pf)- map + filter using a partial function
- zipWithIndex- pair each element with its position
- flatMap(f)- map then flatten one level of nesting
- sliding(n)- iterate over overlapping windows of size n
- view- lazy, non-strict transformation pipeline
Pro Tip
Prefer immutable `val` and persistent collections (List, Vector) over mutable ones — Scala's collections are optimized for structural sharing, so copies are cheap.
Was this cheat sheet helpful?
Explore Topics
#Scala#ScalaCheatSheet#Programming#Advanced#BasicSyntax#Case#Classes#Pattern#OOP#DataStructures#CheatSheet#SkillVeris