Language basics
27 steps in 9 sets of Scala.
Scala runs functional programming on the JVM at industrial scale: Spark, Kafka, and half the big-data stack are written in it, and it compiles to the same bytecode Java does while saying twice as much per line. Scala 3 is the dialect here — the braceless, indentation-based syntax with enums, extension methods, and top-level definitions that made the language feel a decade younger.
This tour covers the everyday core: vals and type inference, expressions everywhere (if and match return values), methods and lambdas, the immutable collections and their map/filter/fold toolkit, case classes with copy, enums, Option and Either instead of null and exceptions, and traits with extension methods. The encore ships complete programs. Every seed compiles under scalac -Werror with unused-checking on, and the encore runs start to finish.
Values & types
- val & varA ferry seat count held in
valbindings, with onevarfor the number that really changes. - Type inferenceThe compiler reads
Int,Double,StringandListtypes off the values you write. - String interpolationThree interpolators:
sfor values,ffor formatted output,rawfor untouched escapes.
Expressions
- if as an expression
if/elseevaluates to a value in Scala, so it can be assigned straight to aval. - match basicsMatching on plain values, with alternatives, guards, and a catch-all.
- for loopsThree shapes of
for: over a collection, over.indices, and with two generators.
Functions
- Defining methods
defwith typed parameters and a return type, first as an expression and then as an indented body. - Default & named argumentsParameters that carry defaults, and calls that name their arguments instead of ordering them.
- Lambdas & function valuesFunction literals, the
_shorthand, and a method that takes a function as a parameter.
Collections
Transformations
- Map and filterBakery order counts run through
mapandfilter, alone and chained. - FoldingCrate weights collapsed to a total, then to a string and to a
Map. - For comprehensionsOrchard rows turned into new collections with
forandyield.
Case classes & enums
- Case classesA
Lighthouserecord with generated printing and equality, taken apart by a match. - Copy updatesKiln firings revised with
copy, which builds a new value and leaves the old one alone. - EnumsA signal enum matched exhaustively, and a second enum whose cases carry data.
Option & Either
- OptionScala's answer to null: a value that is either there or admits it is not.
- Chaining OptionsTwo dependent lookups and a test, with no null check anywhere.
- Either for validationEither keeps the reason a value was rejected, where a bare Option cannot.
Traits & extensions
- TraitsA trait fixes what every shape must answer and writes the shared part once.
- Extension methodsAdding methods to
IntandStringwithout owning either type. - Objects and companionsAn object is a namespace with one instance; a companion is where constructors go.
Encore
- word_freq.scalaA whole report: normalize a passage, count its words, rank and print them.
- grade_report.scalaPer-student averages, letter grades and a class summary from an inline roster.
- temp_stats.scalaOne week of high temperatures as a table of stats and a bar chart.