13 External effects and I/O
This chapter covers
- Contrasting effects and side effects
- Introducing trampolining
- Discussing free monads
- Defining capability traits
In this chapter, we’ll take what we’ve learned so far about monads and algebraic data types and extend it to handling external effects, like reading from databases and writing to files. We’ll develop a monad for I/O, aptly called IO
, that will allow us to handle such external effects in a purely functional way.
We’ll make an important distinction in this chapter between effects and side effects. The IO
monad provides a straightforward way of embedding imperative programming with I/O effects in a pure program, while preserving referential transparency. It clearly separates effectful
Get Functional Programming in Scala, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.