Scala Demo Examples

Practice programs, one file each. Every demo is self-contained, commented, and needs nothing but a JDK and scala-cli — see Setup & Toolchain.

How to use: click View to open a file in the code viewer, read its comments, download it, then run it on your machine.

scala-cli run 01_hello.scala    # compile and run a demo
scala-cli test 18_testing.scala # run the MUnit test demo

Foundations

Values, types, strings — the vocabulary of the language.

#DescriptionLink
1Hello, WorldView
2Variables: val vs varView
3Scalar Types & CastingView
4Strings & FormattingView

Functions & Control Flow

Named and anonymous functions, branching, loops, and collections.

#DescriptionLink
5Functions & Higher-Order FunctionsView
6if/else and matchView
7for, while, labeled breakView
8List, Map, Set & PipelinesView

Classes, Inheritance & Traits

The object-oriented side of Scala.

#DescriptionLink
9Classes & ConstructorsView
10Inheritance & OverrideView
11TraitsView
12Case ClassesView
13Companion ObjectsView

Generics & Functional Error Handling

Type parameters, algebraic data types, and Option/Try/Either.

#DescriptionLink
14Option, Try & EitherView
15GenericsView
16Sealed Traits & Exhaustive MatchingView

Professional

Futures and tests — the skills a production codebase expects.

#DescriptionLink
17Concurrency with FutureView
18Unit Tests (MUnit)View

Study Projects

Small programs that compose several lessons into one working piece.

#DescriptionLink
19Word Frequency CounterView
20In-Memory Todo ListView
Practice move: after running each demo, change one thing on purpose — drop a case from a sealed trait match, call .get on a None, remove the implicit ExecutionContext — and read the compiler or runtime error before fixing it.