Scala from the ground up
One language for scripts and services
- Beginner
- 12 chapters
- 192 min read
Learn Scala by running it: the object that makes a program, values and types, text with s"...", if and match as expressions, for and while, functions and function values, classes and case classes, lists and their methods, maps and sets, traits, Option, and a capstone that groups records into a sorted, padded report.
What You'll Learn
- Write and run a program that starts as an object extending App and prints a line
- Hold values in val and var, and know what whole-number division really does
- Build text with s"..." interpolation and use length, trim, split and contains
- Branch with if as an expression, and match on numbers, strings and guards
- Repeat work with to, until, by and while, and loop over a list
- Write defs with parameters, return types, defaults and named arguments
- Build classes with constructors, and case classes with copy and pattern matching
- Transform lists with map, filter, foldLeft, sum, sortBy and mkString
- Look values up in a Map and a Set, with get, getOrElse and updated
- Share behaviour with a trait, and print a list of the trait type polymorphically
- Answer questions that may be empty with Option, getOrElse, map and flatMap
- Assemble a grouped, sorted and padded report from case classes
Prerequisites
- No programming experience needed — this course starts at the first line
- Any other language you know will make the early chapters quick
Course Content
- 01Your first Scala programNot started
What Scala is, the object with a main body every program starts from, and how to print a line of text on the screen.
- 02Values, variables and typesNot started
val for a name you set once and var for one you change, the four types you will use daily, and what division really does to whole numbers.
- 03Text and string interpolationNot started
Text as a value you can ask questions of, the s before the quotes that puts a name inside a sentence, and the methods that measure, trim and split a line.
- 04Decisions: if and matchNot started
if as an expression that hands back a value, match as a decision table over numbers and strings, guards, and the underscore that catches everything left.
- 05Loops: for and whileNot started
Ranges with to, until and by, a while loop with a counter that has to move, a for over a list, and building a line of text one pass at a time.
- 06Functions and function valuesNot started
def with parameters and a return type, defaults and named arguments, a function held in a val, and a lambda handed straight to map.
- 07Classes and case classesNot started
A class with a constructor and its own toString, then the same record as a case class, where copy, value equality and pattern matching come for free.
- 08Lists and their methodsNot started
An ordered collection that never changes, and the methods that do the looping for you: map, filter, foldLeft, sum, sortBy, mkString, zip, take and drop.
- 09Maps and setsNot started
A Map for lookups by key and a Set for values that appear once, with get, getOrElse and updated, and a sorted print so the output is the same every run.
- 10Traits and shared behaviourNot started
A trait that states what a type can do, one method written once inside it, two classes that fill in the blanks, and a list of the trait type printed by one loop.
- 11Option, or a value that may be missingNot started
Some and None, getOrElse for a fallback, map and flatMap for chains of lookups, a match to open one by hand, and a function that says in its type that it may find nothing.
- 12Putting it together: a sales reportNot started
A case class for the rows, groupBy and a sum per group, sortBy for a fixed order, and f"..." with padding so the numbers line up in columns.
