0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Transformations

map, filter, flatMap.

Transformations is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Transforming collections

Scala collections are immutable by default. Instead of mutating them, you transform them into new collections using methods like map, filter, and flatMap.

object Main {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4)
    val doubled = nums.map(_ * 2)
    println(nums)
    println(doubled)
  }
}

map: transform every element

map applies a function to each element and returns a new collection of the same size. The element type may change.

object Main {
  def main(args: Array[String]): Unit = {
    val words = List("scala", "rocks")
    val lengths = words.map(_.length)
    println(lengths)
  }
}

The underscore shorthand

The underscore _ is shorthand for a single function parameter. _ * 2 means x => x * 2. It keeps simple transformations compact.

object Main {
  def main(args: Array[String]): Unit = {
    val nums = List(10, 20, 30)
    println(nums.map(_ + 1))
    println(nums.map(x => x + 1))
  }
}

filter: keep matching elements

filter keeps only the elements for which the predicate returns true. The result may be smaller than the original.

object Main {
  def main(args: Array[String]): Unit = {
    val nums = (1 to 10).toList
    val evens = nums.filter(_ % 2 == 0)
    println(evens)
  }
}

filterNot: the opposite

filterNot keeps elements where the predicate is false. It is the complement of filter.

object Main {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5)
    println(nums.filterNot(_ % 2 == 0))
  }
}

flatMap: map then flatten

flatMap applies a function that returns a collection for each element, then flattens the results into one collection.

object Main {
  def main(args: Array[String]): Unit = {
    val words = List("ab", "cd")
    val chars = words.flatMap(_.toList)
    println(chars)
  }
}

map vs flatMap

If your function returns a collection, map gives you nested collections while flatMap gives you a flat one. Choose based on the shape you want.

object Main {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3)
    println(nums.map(n => List(n, n)))
    println(nums.flatMap(n => List(n, n)))
  }
}

flatMap with Option

flatMap over a list of Option drops the None values and unwraps the Some values, a handy way to filter and transform at once.

object Main {
  def main(args: Array[String]): Unit = {
    val raw = List("1", "x", "3", "y")
    val parsed = raw.flatMap(_.toIntOption)
    println(parsed)
  }
}

collect: filter and map together

collect takes a partial function: it keeps and transforms only the elements the function is defined for, combining filter and map in one pass.

object Main {
  def main(args: Array[String]): Unit = {
    val mixed: List[Any] = List(1, "two", 3, "four", 5)
    val ints = mixed.collect { case i: Int => i * 10 }
    println(ints)
  }
}

Chaining transformations

Because each method returns a new collection, you can chain them into a readable pipeline. Each step transforms the output of the previous one.

object Main {
  def main(args: Array[String]): Unit = {
    val result = List(1, 2, 3, 4)
      .map(_ + 1)
      .filter(_ % 2 == 0)
    println(result)
  }
}

A clean pipeline

Here is a tidy transformation pipeline: filter even numbers, square them, then keep the large ones. Pipelines read like a description of the data flow.

object Main {
  def main(args: Array[String]): Unit = {
    val result = (1 to 10).toList
      .filter(_ % 2 == 0)
      .map(x => x * x)
      .filter(_ > 10)
    println(result)
  }
}

Quick Check

What does List("ab", "cd").flatMap(_.toList) return?

Recap

You learned the core transformations:

  • map — transform each element, same size
  • filter / filterNot — keep or drop by predicate
  • flatMap — map then flatten nested results
  • collect — filter and map with a partial function
  • Chain them into readable, immutable pipelines

Frequently asked questions

Is the “Transformations” lesson free?

Yes — the full text of “Transformations” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Transformations”?

map, filter, flatMap. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Transformations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. List, Vector, Set, Map
  2. Transformations
  3. Folding and Reducing
  4. Grouping and Sorting
← Back to Scala for Backend Engineering & Functional Programming