Chapter 16. Developing reactive functional programs

 

This chapter covers

  • Reacting to events and declaring them in F#
  • Working with events as first-class values
  • Programming GUIs using asynchronous workflows
  • Using message passing concurrency

This final chapter is about reacting to external events, such as user input. We’re also going to discuss another F# feature that can be used for creating concurrent programs. Although these sound like unrelated topics, we’ll see similarities as we explore them. All of the libraries and examples we’ll see in this chapter share a similar architecture, so let’s first look at the reactive architecture in general.

A lot of the code we write (in both imperative and functional programming) assumes that it’s in the driving seat—that we’re in control of what happens at each step. This model often breaks down for UIs. A Windows application needs to handle a variety of UI events; it may also have to respond to the completion of an asynchronous web service request or background task. The execution of this type of application is controlled by the events, and the application is concerned with reacting to them. For this reason, this principle is sometimes called inversion of control and is sometimes lightheartedly referred to as the Hollywood Principle.[1]

1 “Don’t call us, we’ll call you.”

16.1. Reactive programming using events

16.1.1. Introducing event functions

16.1.2. Using events and observables

16.1.3. Creating a simple reactive application

16.1.4. Declarative event processing using LINQ

16.1.5. Declaring events in F#

16.2. Creating reactive animations

16.2.1. Using the switch function

16.2.2. Implementing the switch function

16.3. Programming UIs using workflows

16.3.1. Waiting for events asynchronously

16.3.2. Drawing rectangles

16.4. Storing state in reactive applications

16.4.1. Working with state safely

16.4.2. Creating a mailbox processor

16.4.3. Communicating using messages

16.4.4. Encapsulating mailbox processors

16.4.5. Waiting for multiple events

16.5. Message passing concurrency

16.5.1. Creating a state machine processor

16.5.2. Accessing mailbox concurrently

16.6. Summary