Scala Functional Programming Cheat Sheet
Covers Scala functions and currying, pattern matching, functional collection operations, and core functional programming concepts like immutability.
2 PagesIntermediateApr 10, 2026
Functions & Currying
Function values, higher-order functions, and partial application.
scala
// Function values and higher-order functionsval square: Int => Int = x => x * xdef applyTwice(f: Int => Int, x: Int): Int = f(f(x))applyTwice(square, 3) // => 81// Partial application / curryingdef add(a: Int)(b: Int): Int = a + bval addFive = add(5) _addFive(10) // => 15// Immutable valuesval name: String = "Scala" // val is immutable; var is mutable
Pattern Matching
Destructuring case classes and matching with guards.
scala
sealed trait Shapecase class Circle(radius: Double) extends Shapecase class Rectangle(width: Double, height: Double) extends Shapedef area(shape: Shape): Double = shape match { case Circle(r) => math.Pi * r * r case Rectangle(w, h) => w * h}// Matching with guardsdef describe(n: Int): String = n match { case 0 => "zero" case x if x > 0 => "positive" case _ => "negative"}
Functional Collections
map, filter, fold, and for-comprehensions.
scala
val numbers = List(1, 2, 3, 4, 5)val doubled = numbers.map(_ * 2) // List(2, 4, 6, 8, 10)val evens = numbers.filter(_ % 2 == 0) // List(2, 4)val total = numbers.foldLeft(0)(_ + _) // 15val sum = numbers.sum // 15// for-comprehensionval pairs = for { x <- List(1, 2) y <- List("a", "b")} yield (x, y)// List((1,a), (1,b), (2,a), (2,b))
Core FP Concepts
Foundational ideas behind Scala's functional style.
- Immutability- val bindings and immutable collections (List, Vector) are the default; prefer them over var and mutable collections
- case class- Auto-generates equals, hashCode, toString, and copy(); ideal for immutable data
- Option[T]- Represents an optional value as Some(value) or None, avoiding null and NullPointerException
- Higher-order functions- Functions that take or return other functions, e.g. map, filter, and foldLeft
- for-comprehension- Syntactic sugar over flatMap/map/withFilter for chaining monadic operations like Option, List, and Future
- Pattern matching- match expressions destructure case classes and sealed traits, with exhaustiveness checked at compile time
Pro Tip
Use sealed traits with case classes for algebraic data types — the compiler warns on non-exhaustive match expressions, catching missing cases at compile time instead of runtime.
Was this cheat sheet helpful?
Explore Topics
#ScalaFunctionalProgramming#ScalaFunctionalProgrammingCheatSheet#Programming#Intermediate#FunctionsCurrying#PatternMatching#FunctionalCollections#CoreFPConcepts#Functions#DataStructures#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance