concept side effect in category scala

This is an excerpt from Manning's book Functional Programming in Scala.
Listing 1.1. A Scala program with side effects
class Cafe { #1 def buyCoffee(cc: CreditCard): Coffee = { #2 val cup = new Coffee() #3 cc.charge(cup.price) #4 cup #5 } } #1: The class keyword introduces a class, much like in Java. Its body is contained in curly braces, { and }. #2: A method of a class is introduced by the def keyword. cc: CreditCard defines a parameter named cc of type CreditCard. The Coffee return type of the buyCoffee method is given after the parameter list, and the method body consists of a block within curly braces after an = sign. #3: No semicolons are necessary. Newlines delimit statements in a block. #4: Side effect. Actually charges the credit card. #5: We don’t need to say return. Since cup is the last statement in the block, it is automatically returned.
Suppose we’re implementing a program to handle purchases at a coffee shop. We’ll begin with a Scala program that uses side effects in its implementation (also called an impure program).
Listing 1.1. A Scala program with side effects
class Cafe { #1 def buyCoffee(cc: CreditCard): Coffee = { #2 val cup = new Coffee() #3 cc.charge(cup.price) #4 cup #5 } } #1: The class keyword introduces a class, much like in Java. Its body is contained in curly braces, { and }. #2: A method of a class is introduced by the def keyword. cc: CreditCard defines a parameter named cc of type CreditCard. The Coffee return type of the buyCoffee method is given after the parameter list, and the method body consists of a block within curly braces after an = sign. #3: No semicolons are necessary. Newlines delimit statements in a block. #4: Side effect. Actually charges the credit card. #5: We don’t need to say return. Since cup is the last statement in the block, it is automatically returned.The line cc.charge(cup.price) is an example of a side effect. Charging a credit card involves some interaction with the outside world—suppose it requires contacting the credit card company via some web service, authorizing the transaction, charging the card, and (if successful) persisting some record of the transaction for later reference. But our function merely returns a Coffee and these other actions are happening on the side, hence the term “side effect.” (Again, we’ll define side effects more formally later in this chapter.)
The preceding section makes it clear that pure functions may have side effects with respect to data that’s locally scoped. The quicksort function may mutate the array because it allocated that array, it’s locally scoped, and it’s not possible for any outside code to observe the mutation. If, on the other hand, quicksort somehow mutated its input list directly (as is common in mutable collection APIs), that side effect would be observable to all callers of quicksort.
There’s nothing wrong with doing this sort of loose reasoning to determine the scoping of side effects, but it’s sometimes desirable to enforce effect scoping using Scala’s type system. The constituent parts of quicksort would have direct side effects if used on their own, and with the types we’re using, we get no help from the compiler in controlling the scope of these side effects. Nor are we alerted if we accidentally leak side effects or mutable state to a broader scope than intended. In this section, we’ll develop a data type that uses Scala’s type system to enforce scoping of mutations.[1]

This is an excerpt from Manning's book Scala in Action.
Another aspect of functional programming is that it doesn’t have side effects or mutability. The benefits of not having mutability and side effects in functional programs are that the programs are much easier to understand (it has no side effects), reason about, and test because the activity of the function is completely local and it has no external effects. Another huge benefit of functional programming is ease of concurrent programming. Concurrency becomes a nonissue because there’s no change (immutability) to coordinate between processes or threads. You’ll learn about the functional programming side of Scala throughout the book, particularly in chapter 10.
But what about the functions that depend on some external state and don’t return the same result all the time? They’re functions but they’re not pure functions. A pure function doesn’t have side effects. Any observable behavior change after the function finishes is considered a side effect. Updating global or static variables, writing data to the filesystem, displays on screen, calling other “side-effecting” functions, and throwing exceptions are all examples of side effects. The behavior of a pure function doesn’t depend on any external behavior or state. You’re also not allowed to mutate the arguments to the function or invoke a function that has side effects. The add function is a perfect example of a pure function, but the following weather function isn’t a pure function because the weather will change based on when you invoke the function:
So far, I’ve been focusing on implementing pure functional solutions, but what if you have to deal with side effects like writing to a socket or to a database? You can’t avoid that, and in fact any useful program you write for an enterprise probably has a side effect. Are you doomed? No! The trick is to push the side effects as far down as possible. You can create an impure layer, as shown in figure 5.2, and keep the rest of your application as pure and functional as possible. In section 5.5 you’ll learn how to build abstractions around code that by necessity include side effects.
Figure 5.2. Separating pure and side-effecting (impure) code. The side-effecting code should form a thin layer around the application.
![]()
To demonstrate how this works, you’re going to build a simple HTTP server that only serves files from a directory in which the server is started. You’re going to implement the HTTP GET command. Like any server, this HTTP server is full of side effects, like writing to a socket, reading files from the filesystem, and so on. Here are your design goals for the server you’re building:
At first glance it may seem odd to compare object-oriented and functional programming at a pure versus impure level. Although it’s true that you can write object-oriented code without side effects, in practice OOP easily becomes riddled with undesirable side effects. Typically, OO-style applications are built around the idea of mutable state (produces side effects) managed by various objects inside the application.