Chapter 7. Designing data-centric programs

 

This chapter covers

  • Representing and processing documents
  • Designing immutable data structures
  • Converting between data representations
  • Using records and recursive discriminated unions

When designing a functional program, first think about the data that the program works with. Because nontrivial programs use data, this phase is extremely important in application design. When implementing a program in a functional language, we also begin with the data structures that we’ll use in the code, then write operations to manipulate the data as the second step.

This is different from object-oriented design, where data is encapsulated in the state of the objects; processing is expressed as methods that are part of the objects and interact with other objects involved in the operation. Most functional programs are data-centric, which means that data is clearly separated from operations. Adding a new operation to work with the data is a matter of writing a single function.

7.1. Functional data structures

7.1.1. Using the F# record type

7.1.2. Functional data structures in C#

7.2. Flat document representation

7.2.1. Drawing elements

7.2.2. Displaying a drawing on a form

7.3. Structured document representation

7.3.1. Converting representations

7.3.2. XML document representation

7.4. Writing operations

7.4.1. Updating using a map operation

7.4.2. Calculating using an aggregate operation

7.5. Object-oriented representations

7.5.1. Representing data with structural patterns

7.5.2. Adding functions using the visitor pattern

7.6. Summary