concept event in category functional programming

This is an excerpt from Manning's book Grokking Functional Programming.
Event has a name, start year and end year. Obviously, there are some rules that Event needs to follow:
5.35 Coffee Break Explained: Parsing with
OptionWe can approach writing validateLength in the same way we approached the other three functions:
def validateName(name: String): Option[String] = if (name.size > 0) Some(name) else None def validateEnd(end: Int): Option[Int] = if (end < 3000) Some(end) else None def validateStart(start: Int, end: Int): Option[Int] = if (start <= end) Some(start) else None

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.
There is a third option, which is to use F# to model your data objects. When coding functionally, it’s natural to separate data from behavior: data is encoded in data types that have little or no behavior (they typically represent the events, view models, data-transfer objects, entities, and value objects of your domain), whereas behavior is encoded in functions that transform that data or perform side effects.
Events are really simple, and they should be really simple. They’re just plain data objects that capture the minimum amount of information required to faithfully represent what happened. For example, here are some events that represent things that may affect a bank account.
Now let’s see how states and events are combined in state transitions. Once you have a state and an event, you can compute the next state by applying the event to the state. This computation is called a state transition, and it’s a function whose signature has this general form:
state → event → stateIn other words, “Give me a state and an event, and I’ll compute the new state after the event.” Particularized for our scenario, this signature becomes
AccountState → Event → AccountStateHere, Event is the base class from which all our events derive, so an implementation must pattern match on the type of the event and then compute a new AccountState with the relevant changes.
Now that you’ve seen how to represent states and events, and how to combine them with state transitions, you’re ready to see how an entity’s current state can be computed from the history of events that affected that entity in the past.
Let’s look at the signature first. We’re taking a sequence of events: the entity’s history. This is the list of events you get from the DB when you query all events for a given EntityID. I’m assuming that the sequence is sorted: events that happened first should be at the top of the list. You must enforce this when you retrieve events from the database, and this often comes for free: because events are persisted as they occur, they’re appended in order, and this ordering is normally preserved when they’re retrieved.
The desired return value is an AccountState, but here it’s wrapped in an Option. This is where the previously discussed implementation of Match for IEnumerable comes in handy. If the history is empty, which means that there’s no recorded history of that entity, the entity doesn’t exist in the system, so the code returns None. Otherwise the sequence must contain a CreatedAccount at the head of the list, with the tail containing all following events. In this case, the code creates the initial state from the CreatedAccount event and uses that as an accumulator to Aggregate, which applies all subsequent events to the state, finally obtaining the current state.
Notice that if you wanted to see not the current state of the account, but its state at any point in the past, this can be done trivially by evaluating the very same function but filtering to exclude events past the desired date. For this reason, event sourcing is a very valuable model when you need an audit trail and to see how an entity has changed through time.
There are two main approaches to immutable storage:
- Event-based—The DB is an ever-growing collection of events.
- Assertion-based—The DB is an ever-growing collection of facts.
Event sourcing means persisting event data as events occur. The state of an entity need not be stored, because it can always be computed as the “sum” of all events that affected the entity. An event-sourced system naturally separates the concerns of reading and writing data, enabling a CQRS architecture that separates between
- The command side, where commands are received, validated, and turned into events that are persisted and published.
- The query side, where events are combined to create view models, which are served to clients and, optionally, cached for better performance.
Event-sourced systems have several main components:
- Commands—Simple, immutable data objects encapsulating a request from a client program.
- Events—Simple, immutable data objects capturing what happened.
- States—Data objects representing the state of an entity at a certain point in time.
- State transitions—Functions that take a state and an event, and produce a new state.
- View models—Data objects for populating views. They’re computed from events.
- Event handlers—These subscribe to events to perform business logic (on the command side) or to update view models (on the query side).
