Chapter 6. Processing values using higher-order functions

 

This chapter covers

  • Working with tuples options and lists
  • Writing higher-order functions for our types
  • Using automatic generalization in F#
  • Composing functions and partial application

In the previous chapter, we introduced the most common functional values. You’ve seen how these values can be constructed and how to work with them using pattern matching. Expressing all of the logic explicitly like this can be tedious, especially if the type has a complicated structure.

The types of values composed from one or several simpler values include tuples and options from the previous chapter, but also lists from chapter 3. In general, tuples are formed from values of several types. They contain exactly one value of each type. Options can contain zero or one value, and lists contain any finite number of elements. When working with these composed values, we often want to apply some operation to the underlying values. Doing so involves the recurring and boilerplate task of deconstructing the composed value into its components and reconstructing it after we apply the operation.

In this chapter, we’ll see how to process values in an easier way. We’ll do this by writing functions that abstract us from the underlying structure of the value and can be simply parameterized to perform a particular operation with some part of the value. We’ll see that this approach is more concise than using pattern matching explicitly.

6.1. Generic higher-order functions

6.1.1. Writing generic functions in F#

6.1.2. Custom operators

6.2. Working with tuples

6.2.1. Working with tuples using functions

6.2.2. Methods for working with tuples in C#

6.3. Working with schedules

6.3.1. Processing a list of schedules

6.3.2. Processing schedules in C#

6.4. Working with the option type

6.4.1. Using the map function

6.4.2. Using the bind function

6.4.3. Evaluating the example step-by-step

6.4.4. Implementing operations for the option type

6.5. Working with functions

6.5.1. Function composition

6.5.2. Function composition in C#

6.6. Type inference

6.6.1. Type inference for function calls in F#

6.6.2. Automatic generalization

6.7. Working with lists

6.7.1. Implementing list in F#

6.7.2. Understanding type signatures of list functions