Pattern Matching
ELI5 — The Vibe Check
Pattern matching is like a super-powered switch statement that can look inside data structures and pull out the parts you need. Instead of checking 'is this a number?' you can say 'is this a list with exactly 3 items where the first is a string?'
Real Talk
A language feature that allows deconstructing data against patterns, binding variables, and branching based on structure. Pattern matching combines conditional logic, destructuring, and type checking into a single expressive construct. Available in Rust, Elixir, Haskell, Scala, and increasingly in TypeScript (via discriminated unions) and Python (match/case).
Show Me The Code
# Python 3.10+
match command:
case {"action": "move", "direction": d}:
print(f"Moving {d}")
case {"action": "attack", "target": t}:
print(f"Attacking {t}")
case _:
print("Unknown command")
When You'll Hear This
"Rust's pattern matching with exhaustiveness checking catches so many bugs at compile time." / "Python 3.10's match/case finally brought structural pattern matching to the language."
Related Terms
Algebraic Data Types
Algebraic data types are like Lego for type systems.
Discriminated Union
A discriminated union is like a box that could contain a cat, a dog, or a fish — but it has a label on the outside telling you which one.
Elixir
Elixir is like Ruby's syntax mated with Erlang's superpower: millions of tiny processes running independently, each with their own memory.
Rust
Rust is a compiled systems programming language obsessed with memory safety.