Exit e-book
Show all chapters
But … real life applications need to run side effects!
But … real life applications need to run side effects!

Sign up to our Newsletter

Signing up to our newsletter allows you to read all our ebooks.

    Introduction to Programming with ZIO Functional Effects

    But … real life applications need to run side effects!

    Now we know that functional programming is about programming with pure functions, and that pure functions cannot produce side effects, several logical questions arise:

    • How can we then have an application written using functional programming,  which at the same time interacts with external services, such as databases or third-party APIs? Isn’t this somewhat contradictory?
    • Does this mean that functional programming cannot be used in real applications, but only in academic settings?

    The answer to these questions is the following: Yes, we can use functional programming in real applications, and not just in academic settings. For our applications to be able to interact with external services, we can do the following: instead of writing functions that interact with the outside world, we write functions that describe interactions with the outside world, which are executed only at a specific point in our application, (usually called the end of the world) for example the main function.

    If we think about this carefully, these descriptions of interactions with the outside world are simply immutable values that can serve as inputs and outputs of pure functions, and in this way we would not be violating a basic principle of functional programming: do not produce side effects.

    What is this aforementioned end of the world? Well, the end of the world is simply a specific point in our application where the functional world ends, and where the descriptions of interactions with the outside world are being run, usually as late as possible, preferably at the very edge of our program which is its main function.  In this way, our entire application can be written following the functional style, but at the same time capable of performing useful tasks.

    Now that we know all this, a new question arises: how can we write our applications in such a way that all our functions do not execute side effects, but only build descriptions of what we want to do?  And this is where a very powerful library comes in, one that can help us with this task: Introducing the ZIO library.