Understanding FP in the context of an enterprise application (in Scala)

Most of the examples (if not all) that I see are a kind of function that does some kind of calculation and finishes. In this aspect FP shines. However, it’s difficult for me to understand how to apply it in the context of a corporate application environment, where there are not many algorithms, and there is a lot of data and services transfer.

So I would like to ask how to implement the following FP style problem.

I want to implement an event bus service. The service has a register method for registering listeners and publish for publishing events.

In OO configuration, this is done by creating an EventBus interface with both methods. Then the implementation can use a list to store listeners, which is updated by register and used in publish . Of course, this means that register has a side effect. Spring can be used to create a class and pass it an instance to publishers or event subscribers.

How to simulate this in FP, given that the event bus service clients are independent (for example, not all of them are created using the "test" method)? Since I see that this negates the creation of the register, they return a new instance of EventBus, since other clients already contain a link to the old instance (and, for example, posting to it will be published only for listeners that he knows about)

I prefer the solution to be in Scala.

+6
scala functional-programming
source share
2 answers

The sketch of the solution is that the publication should return IO[Unit] . Students must be iterative. Registration also returns IO[Unit] .

0
source share

I think you should study the functional methods of reactive programming more closely. Since you want something in Scala, I suggest reading the Cancel the Observer Pattern by Ingo Mayer, Tirk Romph and Martin Odersky.

+4
source share

All Articles