7 Working with Collections
This chapter covers
- Higher order functions
- Functional collection pipelines
- F# Collections
- Aggregations
7.1 What are collections?
Up until now, we've only really looked at individual items of data – also known as scalars. This chapter looks at how we work with multiple items of data at once. We do this all the time – working with a list of orders or customers etc.. Collections are especially important in F# and in functional programming languages in general, because a lot of what we do involved thinking of things in terms of data transformations.
7.2 Higher Order Functions
Before we dive into collections, there’s a kind of “pre-requisite” to understanding them which is one of the ways that you compose code together in F# (there are others that we’ll see towards the end of this book) known as Higher order functions (HOFs). These are functions that take another function as an argument. In most languages that support even limited FP-style features, this is something that you will probably have seen.
One of the uses of HOFs is for implementing some common algorithm that operates over some “varying” logic – in the OO world you would probably use either Template or Strategy design pattern. In the FP world, it’s fairly easy to achieve the same thing with functions – here’s an example of a simple calculation function that logs its output to the console:
let executeCalculation a b =
let answer = a + b
printfn $"Adding {a} and {b} = {answer}"
answer