🎓 A1Learner
🔍
0 of 48 lessons completed

Scala Intermediate Introduction

Scala Intermediate is the next level of Scala learning. It builds on the beginner level with stronger types, deeper functions, and programs that do more than one thing at a time.

A typed list holds numbers you can add

The list below holds only integers. Its sum is a number the reader can check.

@main def main(): Unit =
  val scores = List(90, 80, 70)
  println(scores.sum)
  println(scores.length)

A function can live in a value

The doubling function is stored in a value. Then map uses that value on every item.

@main def main(): Unit =
  val double = (x: Int) => x * 2
  println(List(1, 2, 3).map(double))

Two results can be built independently

Each list is built from its own starting list. Neither result depends on the other.

@main def main(): Unit =
  val a = List(1, 2, 3).map(_ * 2)
  val b = List(10, 20).map(_ + 1)
  println(a)
  println(b)

Example

@main def main(): Unit =
  val prices = List(10, 20, 30)
  val double = (x: Int) => x * 2
  val doubled = prices.map(double)
  println(doubled)
  println(doubled.sum)

Key points

  • Types as guarantees: a precise type rules out whole groups of mistakes before a program runs.
  • Functions as values: a function can be stored in a value and passed to another function.
  • Programs side by side: later lessons run independent work at the same time instead of one step after another.
  • Generics first: the first group of lessons makes one piece of code work for many types.
  • Depth over breadth: each lesson goes deeper into one idea instead of covering many ideas at once.

In this example, prices is a typed list of integers, double is a function stored in a value, map builds a new list with that function, and the sum of the new list is printed.

Note

this course assumes the beginner course is complete, so beginner ideas appear only as brief reminders.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Scastie

Exercise

What is the main change at the Scala Intermediate level?