Lesson 16. Useful collection functions

 

Now that you have a reasonably high-level understanding of collections in F#, this lesson focuses on getting your muscle memory trained to use collections in practical situations. This lesson covers the following:

  • The most common collection functions across the three types that you’ve learned about (Seq, List, and Array) with some visualizations and hands-on examples
  • A comparison of F# functions with similar LINQ operations
  • The differences between imperative and declarative solutions
  • Moving between collection types

Each operation we cover has a simple example associated with it, alongside typical use cases and equivalents in both imperative coding and LINQ (if it exists). Go through every example in a script yourself rather than simply reading them; you’ll learn these effectively only through practical experience. After that, I’ll also point out some other related functions in the collection libraries (denoted by see also) that you should look at in your own time.

A quick note: as in LINQ, most of the methods in F# collections operate on empty collections without a problem; you’ll get back an empty collection again.

16.1. Mapping functions

Mapping functions take a collection of items and return another collection of items. Usually the mapping can be controlled in some way by the caller, but specialized forms of mapping can be used here as well.

16.1.1. map

16.2. Grouping functions

16.3. More on collections

Summary