Composing IO
Sequencing effects.
Composing IO is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 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.
Sequencing with flatMap
Because IO is a monad, you compose effects with flatMap. Each step runs in order, and a later step can use the result of an earlier one.
import cats.effect.IO
val program: IO[Unit] =
IO("Alice").flatMap(name => IO.println(s"Hello, $name"))for-comprehension
A for-comprehension desugars to flatMap/map and reads like an imperative script while staying pure.
import cats.effect.IO
val prog: IO[Unit] = for {
_ <- IO.println("What is your name?")
name <- IO("Bob")
_ <- IO.println(s"Hi $name")
} yield ()Order Is Preserved
Effects in a for-comprehension run top to bottom when the program is executed. The structure of your code mirrors execution order.
Combining Independent Effects
When two effects do not depend on each other, use mapN or (a, b).tupled to combine them. They still run sequentially by default but express independence.
import cats.effect.IO
import cats.syntax.apply._
val combined: IO[Int] = (IO(2), IO(3)).mapN(_ + _)Running Effects in Parallel
Use parMapN (from cats.syntax.parallel) to run independent effects concurrently on separate fibers and combine their results.
import cats.effect.IO
import cats.syntax.parallel._
val par: IO[Int] = (IO(2), IO(3)).parMapN(_ + _)Discarding Results
Use *> to run an effect and keep the second result, or <* to keep the first. void discards the result entirely, yielding IO[Unit].
import cats.effect.IO
val r1: IO[Int] = IO.println("log") *> IO(42)
val r2: IO[Unit] = IO(99).voidRepeating Effects
replicateA(n) runs an effect n times and collects results into a list. foreverM repeats indefinitely.
import cats.effect.IO
val three: IO[List[Unit]] = IO.println("tick").replicateA(3)Traversing a List
Turn a List[A] into an effect over a List[B] with traverse. Each element produces an IO, and the results are gathered.
import cats.effect.IO
import cats.syntax.traverse._
val names = List("a", "b", "c")
val prog: IO[List[Unit]] = names.traverse(n => IO.println(n))parTraverse
parTraverse is the parallel version of traverse — each element's effect runs on its own fiber, ideal for concurrent I/O like fetching many URLs.
import cats.effect.IO
import cats.syntax.parallel._
val ids = List(1, 2, 3)
val prog: IO[List[Int]] = ids.parTraverse(id => IO(id * 10))A Composed Program
This program reads a value, doubles it, and prints the result — all composed from small IO steps in a single description.
import cats.effect.{IO, IOApp}
object Doubler extends IOApp.Simple {
def run: IO[Unit] = for {
n <- IO(21)
d = n * 2
_ <- IO.println(s"Doubled: $d")
} yield ()
}Plain Scala Sequencing
For comparison, here is sequential console output in plain Scala — eager and impure, unlike the deferred IO version above.
object Main {
def main(args: Array[String]): Unit = {
val n = 21
val d = n * 2
println(s"Doubled: $d")
}
}Quick Check
Which method runs two independent effects concurrently and combines their results?
Recap
You composed IO programs with:
flatMapandforfor sequencingmapN/parMapNfor independent effects*>,<*,voidfor combining/discardingtraverse/parTraversefor collections
Next: handling errors inside IO.
Frequently asked questions
Is the “Composing IO” lesson free?
Yes — the full text of “Composing IO” 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 “Composing IO”?
Sequencing effects. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Composing IO” 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.