51 Working with the IO type

 

After reading this lesson, you will be able to

  • Manipulate values produced by side effects using the map and flatMap operations
  • Compute multiple IO instances in sequence using for-comprehension
  • Execute side effects in parallel to maximize the resource of your program

In the previous lesson, you learned the basics of the type IO. You’ll now see how to compose smaller side effects to create programs you can lazily describe and run. You’ll master how to use the map and flatMap operations to manipulate values that impure functions produce. You’ll discover how to combine them in sequence using for-comprehension. Also, you’ll see how to run them in parallel using the parSequence method. In the capstone, you’ll combine multiple IO instances to define the operations of your quiz application.

51.1 The map and flatMap operations

The IO type offers implementations for the map and flatMap methods. They are consistent with those for Future and the other types you have encountered thus far. Let’s recap their usage in the following sections. You are going to code using the IO type. Do not forget to add cats-effect as an external dependency for your sbt project by adding the following instruction to your build.sbt file:

51.1.1 The map function

51.1.2 The flatMap function

51.2 For-comprehension

51.3 Parallel execution

Summary

Answers to quick checks