Screen 1 of 5

Higher-Order Functions

Think of these like a music playlist app: you hand it a sorting rule (by artist, by date, by mood), and it sorts accordingly. The app is the higher-order function; the rule is the function you pass in.

// A higher-order function: takes a function as input def applyFunction(f: Int => Int, x: Int): Int = f(x) // Pass in a "double it" rule val result = applyFunction(x => x * 2, 5) // result = 10 // Pass in a "square it" rule val result2 = applyFunction(x => x * x, 5) // result2 = 25
"Give me any function f and a number x, I'll apply f to x."
f: Int => Int means "f takes an Int and returns an Int."
We pass in "multiply by 2" as the rule. Apply to 5 → 10.
Same function, different rule! "Square it" applied to 5 → 25.
This is the power: ONE function handles infinite behaviors.
💡 Key Insight
A higher-order function either (1) takes a function as a parameter, or (2) returns a function as its result. This is the foundation of functional programming — functions are "first-class citizens" like any other value.
Screen 2 of 5

Pattern Matching — Scala's Superpower

Think of pattern matching like a mail sorting machine. Each slot has a pattern — letters go here, packages go there, everything else goes to "Other."

val x = 3 x match { case 1 => println("One") case 2 => println("Two") case _ => println("Other") } // Prints: "Other" // With case classes: case class User(name: String, age: Int) val u = User("Alice", 25) u match { case User("Alice", _) => "Hi Alice!" case User(_, age) if age > 18 => "Adult" case _ => "Unknown" }
Check x: if it's 1, print "One".
If it's 2, print "Two".
_ is the wildcard = "everything else." Like "default" in switch.
The real power: match on STRUCTURE, not just values!
If it's a User named Alice (any age), say hi.
if age > 18 is a guard — extra condition on the match.

Use Cases

• Handling different response types
• Decomposing case classes
• Option (Some/None) handling
• List/tuple extraction

🎯 Interview Tip

"How is pattern matching different from switch?" → It matches on types, structures, and guards — not just values. It also returns a value (it's an expression, not a statement).

Screen 3 of 5

Option, Some & None — No More Null Crashes

Imagine ordering online. You either get a package with your item (Some) or an empty box with a note saying "out of stock" (None). Scala's Option forces you to handle both.

Call findUser()
Returns Option[User]
Pattern Match
Some(user) or None
Handle Both!
Click "Next Step" to walk through how Option works.
def findUser(id: Int): Option[User] = { if (id == 1) Some(User("Alice", 25)) else None } findUser(1) match { case Some(user) => s"Found: ${user.name}" case None => "User not found" }
This function might or might not find a user. It says so in the return type: Option[User].
Found? Wrap in Some(). Not found? Return None.
Pattern match forces you to handle BOTH cases. No surprise null crashes!
Screen 4 of 5

Monads — The Scary Word That's Actually Simple

A monad is like an assembly line conveyor belt. Items go in a box, get transformed at each station, and the box keeps them safe throughout the journey. If one station fails, the belt stops gracefully.

📦 Option is a monad

Wraps a value that might not exist. Chain operations with flatMap — if any step returns None, the whole chain returns None.

⏳ Future is a monad

Wraps a value that will arrive later. Chain transformations that automatically wait for previous results.

📋 List is a monad

Wraps multiple values. flatMap transforms each item and flattens results into one list.

// Chaining with flatMap (the monad magic) val result = Some(5) .flatMap(x => Some(x * 2)) // Some(10) .flatMap(x => Some(x + 3)) // Some(13) // If any step returns None, everything stops val fail = Some(5) .flatMap(x => None) // None .flatMap(x => Some(x + 3)) // None (skipped)
Start with 5 in a box (Some(5)).
Station 1: double it → 10, re-box as Some(10).
Station 2: add 3 → 13, re-box as Some(13).
If ANY station returns None, the belt stops. No crash, just a graceful None.
🎯 Interview Answer
"What is a monad?" → A monad is a design pattern that wraps values and lets you chain transformations with flatMap. Option, Future, and List are all monads. The key rule: if one step fails, the chain fails gracefully.
Screen 5 of 5

Knowledge Check 🧠

Q1: A function takes another function as a parameter. What is it called?
A recursive function
A higher-order function
A companion function
A case function
Q2: Your function returns Option[User]. The user isn't found. What does the function return?
null
An empty User object
None
An exception is thrown
Q3: What happens when you call Some(5).flatMap(_ => None).flatMap(x => Some(x + 1))?
Some(6)
A compile error
None — the chain stops at the first None
Some(1)