layout: post
title: “Choosing between collection functions”
description: “A guide for the perplexed”

categories: []

There’s more to learning a new language than the language itself. In order to be productive, you need to memorize a big chunk of the standard library
and be aware of most of the rest of it. For example, if you know C#, you can pick up Java-the-language quite quickly, but you won’t really get up to speed
until you are comfortable with the Java Class Library as well.

Similarly, you can’t really be effective in F# until you have some familiarity with all the F# functions that work with collections.

In C# there are only a few LINQ methods you need to know1 (Select, Where, and so on).
But in F#, there are currently almost 100 functions in the List module (and similar counts in the Seq and Array modules). That’s a lot!

1 Yes, there are more, but you can get by with just a few. In F# it’s more important to know them all.

If you are coming to F# from C#, then, the large number of list functions can be overwhelming.

So I have written this post to help guide you to the one you want.
And for fun, I’ve done it in a “Choose Your Own Adventure” style!

Choosing between collection functions - 图1

What collection do I want?

First, a table with information about the different kinds of standard collections. There are five “native” F# ones: list, seq, array, map and set,
and ResizeArray and IDictionary are also often used.









































Immutable?Notes
listYes
Pros:

  • Pattern matching available.

  • Complex iteration available via recursion.

  • Forward iteration is fast. Prepending is fast.


Cons:

  • Indexed access and other access styles are slow.


seqYes

Alias for IEnumerable.


Pros:

  • Lazy evaluation

  • Memory efficient (only one element at a time loaded)

  • Can represent an infinite sequence.

  • Interop with .NET libraries that use IEnumerable.


Cons:

  • No pattern matching.

  • Forward only iteration.

  • Indexed access and other access styles are slow.


arrayNo

Same as BCL Array.


Pros:

  • Fast random access

  • Memory efficient and cache locality, especially with structs.

  • Interop with .NET libraries that use Array.

  • Support for 2D, 3D and 4D arrays


Cons:

mapYesImmutable dictionary. Requires keys to implement IComparable.
setYesImmutable set. Requires elements to implement IComparable.
ResizeArrayNoAlias for BCL List. Pros and cons similar to array, but resizable.
IDictionaryYes

For an alternate dictionary that does not requires elements to implement IComparable,
you can use the BCL IDictionary.
The constructor is dict in F#.


Note that mutation methods such as Add are present, but will cause a runtime error if called.


These are the main collection types that you will encounter in F#, and will be good enough for all common cases.

If you need other kinds of collections though, there are lots of choices:

  • You can use the collection classes in .NET, either the traditional, mutable ones
    or the newer ones such as those in the System.Collections.Immutable namespace.
  • Alternatively, you can use one of the F# collection libraries:
    • FSharpx.Collections, part of the FSharpx series of projects.
    • ExtCore. Some of these are drop-in (almost) replacements for the Map and Set types in FSharp.Core which provide improved performance in specific scenarios (e.g., HashMap). Others provide unique functionality to help tackle specific coding tasks (e.g., LazyList and LruCache).
    • Funq: high performance, immutable data structures for .NET.
    • Persistent: some efficient persistent (immutable) data structures.

About the documentation

All functions are available for list, seq and array in F# v4 unless noted. The Map and Set modules have some of them as well, but I won’t be discussing map and set here.

For the function signatures I will use list as the standard collection type. The signatures for the seq and array versions will be similar.

Many of these functions are not yet documented on MSDN so I’m going to link directly to the source code on GitHub, which has the up-to-date comments.
Click on the function name for the link.

Note on availability

The availability of these functions may depend on which version of F# you use.

  • In F# version 3 (Visual Studio 2013), there was some degree of inconsistency between Lists, Arrays and Sequences.
  • In F# version 4 (Visual Studio 2015), this inconsistency has been eliminated, and almost all functions are available for all three collection types.

If you want to know what changed between F# v3 and F# v4, please see this chart
(from here).
The chart shows the new APIs in F# v4 (green), previously-existing APIs (blue), and intentional remaining gaps (white).

Some of the functions documented below are not in this chart — these are newer still! If you are using an older version of F#,
you can simply reimplement them yourself using the code on GitHub.

With that disclaimer out of the way, you can start your adventure!


Table of contents


1. What kind of collection do you have?

What kind of collection do you have?

  • If you don’t have a collection, and want to create one, go to section 2.
  • If you already have a collection that you want to work with, go to section 9.
  • If you have two collections that you want to work with, go to section 23.
  • If you have three collections that you want to work with, go to section 24.
  • If you have more than three collections that you want to work with, go to section 25.
  • If you want to combine or uncombine collections, go to section 26.


2. Creating a new collection

So you want to create a new collection. How do you want to create it?

  • If the new collection will be empty or will have one element, go to section 3.
  • If the new collection is a known size, go to section 4.
  • If the new collection is potentially infinite, go to section 7.
  • If you don’t know how big the collection will be, go to section 8.


3. Creating a new empty or one-element collection

If you want to create a new empty or one-element collection, use these functions:

If you know the size of the collection in advance, it is generally more efficient to use a different function. See section 4 below.

Usage examples

  1. let list0 = List.empty
  2. // list0 = []
  3. let list1 = List.singleton "hello"
  4. // list1 = ["hello"]


4. Creating a new collection of known size

  • If all elements of the collection will have the same value, go to section 5.
  • If elements of the collection could be different, go to section 6.


5. Creating a new collection of known size with each element having the same value

If you want to create a new collection of known size with each element having the same value, you want to use replicate:

Array.create is basically the same as replicate (although with a subtly different implementation!) but replicate was only implemented for Array in F# v4.

Usage examples

  1. let repl = List.replicate 3 "hello"
  2. // val repl : string list = ["hello"; "hello"; "hello"]
  3. let arrCreate = Array.create 3 "hello"
  4. // val arrCreate : string [] = [|"hello"; "hello"; "hello"|]
  5. let intArr0 : int[] = Array.zeroCreate 3
  6. // val intArr0 : int [] = [|0; 0; 0|]
  7. let stringArr0 : string[] = Array.zeroCreate 3
  8. // val stringArr0 : string [] = [|null; null; null|]

Note that for zeroCreate, the target type must be known to the compiler.


6. Creating a new collection of known size with each element having a different value

If you want to create a new collection of known size with each element having a potentially different value, you can choose one of three ways:

  • init : length:int -> initializer:(int -> 'T) -> 'T list.
    Creates a collection by calling the given generator on each index.
  • For lists and arrays, you can also use the literal syntax such as [1; 2; 3] (lists) and [|1; 2; 3|] (arrays).
  • For lists and arrays and seqs, you can use the comprehension syntax for .. in .. do .. yield.

Usage examples

  1. // using list initializer
  2. let listInit1 = List.init 5 (fun i-> i*i)
  3. // val listInit1 : int list = [0; 1; 4; 9; 16]
  4. // using list comprehension
  5. let listInit2 = [for i in [1..5] do yield i*i]
  6. // val listInit2 : int list = [1; 4; 9; 16; 25]
  7. // literal
  8. let listInit3 = [1; 4; 9; 16; 25]
  9. // val listInit3 : int list = [1; 4; 9; 16; 25]
  10. let arrayInit3 = [|1; 4; 9; 16; 25|]
  11. // val arrayInit3 : int [] = [|1; 4; 9; 16; 25|]

Literal syntax allows for an increment as well:

  1. // literal with +2 increment
  2. let listOdd= [1..2..10]
  3. // val listOdd : int list = [1; 3; 5; 7; 9]

The comprehension syntax is even more flexible because you can yield more than once:

  1. // using list comprehension
  2. let listFunny = [
  3. for i in [2..3] do
  4. yield i
  5. yield i*i
  6. yield i*i*i
  7. ]
  8. // val listFunny : int list = [2; 4; 8; 3; 9; 27]

and it can also be used as a quick and dirty inline filter:

  1. let primesUpTo n =
  2. let rec sieve l =
  3. match l with
  4. | [] -> []
  5. | p::xs ->
  6. p :: sieve [for x in xs do if (x % p) > 0 then yield x]
  7. [2..n] |> sieve
  8. primesUpTo 20
  9. // [2; 3; 5; 7; 11; 13; 17; 19]

Two other tricks:

  • You can use yield! to return a list rather than a single value
  • You can also use recursion

Here is an example of both tricks being used to count up to 10 by twos:

  1. let rec listCounter n = [
  2. if n <= 10 then
  3. yield n
  4. yield! listCounter (n+2)
  5. ]
  6. listCounter 3
  7. // val it : int list = [3; 5; 7; 9]
  8. listCounter 4
  9. // val it : int list = [4; 6; 8; 10]


7. Creating a new infinite collection

If you want an infinite list, you have to use a seq rather than a list or array.

  • initInfinite : initializer:(int -> 'T) -> seq<'T>.
    Generates a new sequence which, when iterated, will return successive elements by calling the given function.
  • You can also use a seq comprehension with a recursive loop to generate an infinite sequence.

Usage examples

  1. // generator version
  2. let seqOfSquares = Seq.initInfinite (fun i -> i*i)
  3. let firstTenSquares = seqOfSquares |> Seq.take 10
  4. firstTenSquares |> List.ofSeq // [0; 1; 4; 9; 16; 25; 36; 49; 64; 81]
  5. // recursive version
  6. let seqOfSquares_v2 =
  7. let rec loop n = seq {
  8. yield n * n
  9. yield! loop (n+1)
  10. }
  11. loop 1
  12. let firstTenSquares_v2 = seqOfSquares_v2 |> Seq.take 10


8. Creating a new collection of indefinite size

Sometimes you don’t know how big the collection will be in advance. In this case you need a function that will keep adding elements until it gets a signal to stop.
unfold is your friend here, and the “signal to stop” is whether you return a None (stop) or a Some (keep going).

Usage examples

This example reads from the console in a loop until an empty line is entered:

  1. let getInputFromConsole lineNo =
  2. let text = System.Console.ReadLine()
  3. if System.String.IsNullOrEmpty(text) then
  4. None
  5. else
  6. // return value and new threaded state
  7. // "text" will be in the generated sequence
  8. Some (text,lineNo+1)
  9. let listUnfold = List.unfold getInputFromConsole 1

unfold requires that a state be threaded through the generator. You can ignore it (as in the ReadLine example above), or you can
use it to keep track of what you have done so far. For example, you can create a Fibonacci series generator using unfold:

  1. let fibonacciUnfolder max (f1,f2) =
  2. if f1 > max then
  3. None
  4. else
  5. // return value and new threaded state
  6. let fNext = f1 + f2
  7. let newState = (f2,fNext)
  8. // f1 will be in the generated sequence
  9. Some (f1,newState)
  10. let fibonacci max = List.unfold (fibonacciUnfolder max) (1,1)
  11. fibonacci 100
  12. // int list = [1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89]


9. Working with one list

If you are working with one list and…

  • If you want to get an element at a known position, go to section 10
  • If you want to get one element by searching, go to section 11
  • If you want to get a subset of the collection, go to section 12
  • If you want to partition, chunk, or group a collection into smaller collections, go to section 13
  • If you want to aggregate or summarize the collection into a single value, go to section 14
  • If you want to change the order of the elements, go to section 15
  • If you want to test the elements in the collection, go to section 16
  • If you want to transform each element to something different, go to section 17
  • If you want to iterate over each element, go to section 18
  • If you want to thread state through an iteration, go to section 19
  • If you need to know the index of each element while you are iterating or mapping, go to section 20
  • If you want to transform the whole collection to a different collection type, go to section 21
  • If you want to change the behaviour of the collection as a whole, go to section 22
  • If you want to mutate the collection in place, go to section 27
  • If you want to use a lazy collection with an IDisposable, go to section 28


10. Getting an element at a known position

The following functions get a element in the collection by position:

But what if the collection is empty? Then head and last will fail with an exception (ArgumentException).

And if the index is not found in the collection? Then another exception again (ArgumentException for lists, IndexOutOfRangeException for arrays).

I would therefore recommend that you avoid these functions in general and use the tryXXX equivalents below:

Usage examples

  1. let head = [1;2;3] |> List.head
  2. // val head : int = 1
  3. let badHead : int = [] |> List.head
  4. // System.ArgumentException: The input list was empty.
  5. let goodHeadOpt =
  6. [1;2;3] |> List.tryHead
  7. // val goodHeadOpt : int option = Some 1
  8. let badHeadOpt : int option =
  9. [] |> List.tryHead
  10. // val badHeadOpt : int option = None
  11. let goodItemOpt =
  12. [1;2;3] |> List.tryItem 2
  13. // val goodItemOpt : int option = Some 3
  14. let badItemOpt =
  15. [1;2;3] |> List.tryItem 99
  16. // val badItemOpt : int option = None

As noted, the item function should be avoided for lists. For example, if you want to process each item in a list, and you come from an imperative background,
you might write a loop with something like this:

  1. // Don't do this!
  2. let helloBad =
  3. let list = ["a";"b";"c"]
  4. let listSize = List.length list
  5. [ for i in [0..listSize-1] do
  6. let element = list |> List.item i
  7. yield "hello " + element
  8. ]
  9. // val helloBad : string list = ["hello a"; "hello b"; "hello c"]

Don’t do that! Use something like map instead. It’s both more concise and more efficient:

  1. let helloGood =
  2. let list = ["a";"b";"c"]
  3. list |> List.map (fun element -> "hello " + element)
  4. // val helloGood : string list = ["hello a"; "hello b"; "hello c"]


11. Getting an element by searching

You can search for an element or its index using find and findIndex:

And you can also search backwards:

But what if the item cannot be found? Then these will fail with an exception (KeyNotFoundException).

I would therefore recommend that, as with head and item, you avoid these functions in general and use the tryXXX equivalents below:

If you are doing a map before a find you can often combine the two steps into a single one using pick (or better, tryPick). See below for a usage example.

Usage examples

  1. let listOfTuples = [ (1,"a"); (2,"b"); (3,"b"); (4,"a"); ]
  2. listOfTuples |> List.find ( fun (x,y) -> y = "b")
  3. // (2, "b")
  4. listOfTuples |> List.findBack ( fun (x,y) -> y = "b")
  5. // (3, "b")
  6. listOfTuples |> List.findIndex ( fun (x,y) -> y = "b")
  7. // 1
  8. listOfTuples |> List.findIndexBack ( fun (x,y) -> y = "b")
  9. // 2
  10. listOfTuples |> List.find ( fun (x,y) -> y = "c")
  11. // KeyNotFoundException

With pick, rather than returning a bool, you return an option:

  1. listOfTuples |> List.pick ( fun (x,y) -> if y = "b" then Some (x,y) else None)
  2. // (2, "b")

Pick vs. Find

That ‘pick’ function might seem unnecessary, but it is useful when dealing with functions that return options.

For example, say that there is a function tryInt that parses a string and returns Some int if the string is a valid int, otherwise None.

  1. // string -> int option
  2. let tryInt str =
  3. match System.Int32.TryParse(str) with
  4. | true, i -> Some i
  5. | false, _ -> None

And now say that we want to find the first valid int in a list. The crude way would be:

  • map the list using tryInt
  • find the first one that is a Some using find
  • get the value from inside the option using Option.get

The code might look something like this:

  1. let firstValidNumber =
  2. ["a";"2";"three"]
  3. // map the input
  4. |> List.map tryInt
  5. // find the first Some
  6. |> List.find (fun opt -> opt.IsSome)
  7. // get the data from the option
  8. |> Option.get
  9. // val firstValidNumber : int = 2

But pick will do all these steps at once! So the code becomes much simpler:

  1. let firstValidNumber =
  2. ["a";"2";"three"]
  3. |> List.pick tryInt

If you want to return many elements in the same way as pick, consider using choose (see section 12).


12. Getting a subset of elements from a collection

The previous section was about getting one element. How can you get more than one element? Well you’re in luck! There’s lots of functions to choose from.

To extract elements from the front, use one of these:

To extract elements from the rear, use one of these:

To extract other subsets of elements, use one of these:

To reduce the list to distinct elements, use one of these:

Usage examples

Taking elements from the front:

  1. [1..10] |> List.take 3
  2. // [1; 2; 3]
  3. [1..10] |> List.takeWhile (fun i -> i < 3)
  4. // [1; 2]
  5. [1..10] |> List.truncate 4
  6. // [1; 2; 3; 4]
  7. [1..2] |> List.take 3
  8. // System.InvalidOperationException: The input sequence has an insufficient number of elements.
  9. [1..2] |> List.takeWhile (fun i -> i < 3)
  10. // [1; 2]
  11. [1..2] |> List.truncate 4
  12. // [1; 2] // no error!

Taking elements from the rear:

  1. [1..10] |> List.skip 3
  2. // [4; 5; 6; 7; 8; 9; 10]
  3. [1..10] |> List.skipWhile (fun i -> i < 3)
  4. // [3; 4; 5; 6; 7; 8; 9; 10]
  5. [1..10] |> List.tail
  6. // [2; 3; 4; 5; 6; 7; 8; 9; 10]
  7. [1..2] |> List.skip 3
  8. // System.ArgumentException: The index is outside the legal range.
  9. [1..2] |> List.skipWhile (fun i -> i < 3)
  10. // []
  11. [1] |> List.tail |> List.tail
  12. // System.ArgumentException: The input list was empty.

To extract other subsets of elements:

  1. [1..10] |> List.filter (fun i -> i%2 = 0) // even
  2. // [2; 4; 6; 8; 10]
  3. [1..10] |> List.where (fun i -> i%2 = 0) // even
  4. // [2; 4; 6; 8; 10]
  5. [1..10] |> List.except [3;4;5]
  6. // [1; 2; 6; 7; 8; 9; 10]

To extract a slice:

  1. Array.sub [|1..10|] 3 5
  2. // [|4; 5; 6; 7; 8|]
  3. [1..10].[3..5]
  4. // [4; 5; 6]
  5. [1..10].[3..]
  6. // [4; 5; 6; 7; 8; 9; 10]
  7. [1..10].[..5]
  8. // [1; 2; 3; 4; 5; 6]

Note that slicing on lists can be slow, because they are not random access. Slicing on arrays is fast however.

To extract the distinct elements:

  1. [1;1;1;2;3;3] |> List.distinct
  2. // [1; 2; 3]
  3. [ (1,"a"); (1,"b"); (1,"c"); (2,"d")] |> List.distinctBy fst
  4. // [(1, "a"); (2, "d")]

Choose vs. Filter

As with pick, the choose function might seem awkward, but it is useful when dealing with functions that return options.

In fact, choose is to filter as pick is to find, Rather than using a boolean filter, the signal is Some vs. None.

As before, say that there is a function tryInt that parses a string and returns Some int if the string is a valid int, otherwise None.

  1. // string -> int option
  2. let tryInt str =
  3. match System.Int32.TryParse(str) with
  4. | true, i -> Some i
  5. | false, _ -> None

And now say that we want to find all the valid ints in a list. The crude way would be:

  • map the list using tryInt
  • filter to only include the ones that are Some
  • get the value from inside each option using Option.get

The code might look something like this:

  1. let allValidNumbers =
  2. ["a";"2";"three"; "4"]
  3. // map the input
  4. |> List.map tryInt
  5. // include only the "Some"
  6. |> List.filter (fun opt -> opt.IsSome)
  7. // get the data from each option
  8. |> List.map Option.get
  9. // val allValidNumbers : int list = [2; 4]

But choose will do all these steps at once! So the code becomes much simpler:

  1. let allValidNumbers =
  2. ["a";"2";"three"; "4"]
  3. |> List.choose tryInt

If you already have a list of options, you can filter and return the “Some” in one step by passing id into choose:

  1. let reduceOptions =
  2. [None; Some 1; None; Some 2]
  3. |> List.choose id
  4. // val reduceOptions : int list = [1; 2]

If you want to return the first element in the same way as choose, consider using pick (see section 11).

If you want to do a similar action as choose but for other wrapper types (such as a Success/Failure result), there is a discussion here.


13. Partitioning, chunking and grouping

There are lots of different ways to split a collection! Have a look at the usage examples to see the differences:

Usage examples

  1. [1..10] |> List.chunkBySize 3
  2. // [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]; [10]]
  3. // note that the last chunk has one element
  4. [1..10] |> List.splitInto 3
  5. // [[1; 2; 3; 4]; [5; 6; 7]; [8; 9; 10]]
  6. // note that the first chunk has four elements
  7. ['a'..'i'] |> List.splitAt 3
  8. // (['a'; 'b'; 'c'], ['d'; 'e'; 'f'; 'g'; 'h'; 'i'])
  9. ['a'..'e'] |> List.pairwise
  10. // [('a', 'b'); ('b', 'c'); ('c', 'd'); ('d', 'e')]
  11. ['a'..'e'] |> List.windowed 3
  12. // [['a'; 'b'; 'c']; ['b'; 'c'; 'd']; ['c'; 'd'; 'e']]
  13. let isEven i = (i%2 = 0)
  14. [1..10] |> List.partition isEven
  15. // ([2; 4; 6; 8; 10], [1; 3; 5; 7; 9])
  16. let firstLetter (str:string) = str.[0]
  17. ["apple"; "alice"; "bob"; "carrot"] |> List.groupBy firstLetter
  18. // [('a', ["apple"; "alice"]); ('b', ["bob"]); ('c', ["carrot"])]

All the functions other than splitAt and pairwise handle edge cases gracefully:

  1. [1] |> List.chunkBySize 3
  2. // [[1]]
  3. [1] |> List.splitInto 3
  4. // [[1]]
  5. ['a'; 'b'] |> List.splitAt 3
  6. // InvalidOperationException: The input sequence has an insufficient number of elements.
  7. ['a'] |> List.pairwise
  8. // InvalidOperationException: The input sequence has an insufficient number of elements.
  9. ['a'] |> List.windowed 3
  10. // []
  11. [1] |> List.partition isEven
  12. // ([], [1])
  13. [] |> List.groupBy firstLetter
  14. // []


14. Aggregating or summarizing a collection

The most generic way to aggregate the elements in a collection is to use reduce:

and there are specific versions of reduce for frequently used aggregations:

Finally there are some counting functions:

Usage examples

reduce is a variant of fold without an initial state — see section 19 for more on fold. One way to think of it is just inserting a operator between
each element.

  1. ["a";"b";"c"] |> List.reduce (+)
  2. // "abc"

is the same as

  1. "a" + "b" + "c"

Here’s another example:

  1. [2;3;4] |> List.reduce (*)
  2. // is same as
  3. 2 * 3 * 4
  4. // Result is 24

Some ways of combining elements depend on the order of combining, and so there are two variants of “reduce”:

  • reduce moves forward through the list.
  • reduceBack, not surprisingly, moves backwards through the list.

Here’s a demonstration of the difference. First reduce:

  1. [1;2;3;4] |> List.reduce (fun state x -> (state)*10 + x)
  2. // built up from // state at each step
  3. 1 // 1
  4. (1)*10 + 2 // 12
  5. ((1)*10 + 2)*10 + 3 // 123
  6. (((1)*10 + 2)*10 + 3)*10 + 4 // 1234
  7. // Final result is 1234

Using the same combining function with reduceBack produces a different result! It looks like this:

  1. [1;2;3;4] |> List.reduceBack (fun x state -> x + 10*(state))
  2. // built up from // state at each step
  3. 4 // 4
  4. 3 + 10*(4) // 43
  5. 2 + 10*(3 + 10*(4)) // 432
  6. 1 + 10*(2 + 10*(3 + 10*(4))) // 4321
  7. // Final result is 4321

Again, see section 19 for a more detailed discussion of the related functions fold and foldBack.

The other aggregation functions are much more straightforward.

  1. type Suit = Club | Diamond | Spade | Heart
  2. type Rank = Two | Three | King | Ace
  3. let cards = [ (Club,King); (Diamond,Ace); (Spade,Two); (Heart,Three); ]
  4. cards |> List.max // (Heart, Three)
  5. cards |> List.maxBy snd // (Diamond, Ace)
  6. cards |> List.min // (Club, King)
  7. cards |> List.minBy snd // (Spade, Two)
  8. [1..10] |> List.sum
  9. // 55
  10. [ (1,"a"); (2,"b") ] |> List.sumBy fst
  11. // 3
  12. [1..10] |> List.average
  13. // The type 'int' does not support the operator 'DivideByInt'
  14. [1..10] |> List.averageBy float
  15. // 5.5
  16. [ (1,"a"); (2,"b") ] |> List.averageBy (fst >> float)
  17. // 1.5
  18. [1..10] |> List.length
  19. // 10
  20. [ ("a","A"); ("b","B"); ("a","C") ] |> List.countBy fst
  21. // [("a", 2); ("b", 1)]
  22. [ ("a","A"); ("b","B"); ("a","C") ] |> List.countBy snd
  23. // [("A", 1); ("B", 1); ("C", 1)]

Most of the aggregation functions do not like empty lists! You might consider using one of the fold functions to be safe — see section 19.

  1. let emptyListOfInts : int list = []
  2. emptyListOfInts |> List.reduce (+)
  3. // ArgumentException: The input list was empty.
  4. emptyListOfInts |> List.max
  5. // ArgumentException: The input sequence was empty.
  6. emptyListOfInts |> List.min
  7. // ArgumentException: The input sequence was empty.
  8. emptyListOfInts |> List.sum
  9. // 0
  10. emptyListOfInts |> List.averageBy float
  11. // ArgumentException: The input sequence was empty.
  12. let emptyListOfTuples : (int*int) list = []
  13. emptyListOfTuples |> List.countBy fst
  14. // (int * int) list = []


15. Changing the order of the elements

You can change the order of the elements using reversing, sorting and permuting. All of the following return new collections:

And there are also some array-only functions that sort in place:

Usage examples

  1. [1..5] |> List.rev
  2. // [5; 4; 3; 2; 1]
  3. [2;4;1;3;5] |> List.sort
  4. // [1; 2; 3; 4; 5]
  5. [2;4;1;3;5] |> List.sortDescending
  6. // [5; 4; 3; 2; 1]
  7. [ ("b","2"); ("a","3"); ("c","1") ] |> List.sortBy fst
  8. // [("a", "3"); ("b", "2"); ("c", "1")]
  9. [ ("b","2"); ("a","3"); ("c","1") ] |> List.sortBy snd
  10. // [("c", "1"); ("b", "2"); ("a", "3")]
  11. // example of a comparer
  12. let tupleComparer tuple1 tuple2 =
  13. if tuple1 < tuple2 then
  14. -1
  15. elif tuple1 > tuple2 then
  16. 1
  17. else
  18. 0
  19. [ ("b","2"); ("a","3"); ("c","1") ] |> List.sortWith tupleComparer
  20. // [("a", "3"); ("b", "2"); ("c", "1")]
  21. [1..10] |> List.permute (fun i -> (i + 3) % 10)
  22. // [8; 9; 10; 1; 2; 3; 4; 5; 6; 7]
  23. [1..10] |> List.permute (fun i -> 9 - i)
  24. // [10; 9; 8; 7; 6; 5; 4; 3; 2; 1]


16. Testing the elements of a collection

These set of functions all return true or false.

Usage examples

  1. [1..10] |> List.contains 5
  2. // true
  3. [1..10] |> List.contains 42
  4. // false
  5. [1..10] |> List.exists (fun i -> i > 3 && i < 5)
  6. // true
  7. [1..10] |> List.exists (fun i -> i > 5 && i < 3)
  8. // false
  9. [1..10] |> List.forall (fun i -> i > 0)
  10. // true
  11. [1..10] |> List.forall (fun i -> i > 5)
  12. // false
  13. [1..10] |> List.isEmpty
  14. // false


17. Transforming each element to something different

I sometimes like to think of functional programming as “transformation-oriented programming”, and map (aka Select in LINQ) is one of the most fundamental ingredients for this approach.
In fact, I have devoted a whole series to it here.

Sometimes each element maps to a list, and you want to flatten out all the lists. For this case, use collect (aka SelectMany in LINQ).

Other transformation functions include:

Usage examples

Here are some examples of using map in the conventional way, as a function that accepts a list and a mapping function and returns a new transformed list:

  1. let add1 x = x + 1
  2. // map as a list transformer
  3. [1..5] |> List.map add1
  4. // [2; 3; 4; 5; 6]
  5. // the list being mapped over can contain anything!
  6. let times2 x = x * 2
  7. [ add1; times2] |> List.map (fun f -> f 5)
  8. // [6; 10]

You can also think of map as a function transformer. It turns an element-to-element function into a list-to-list function.

  1. let add1ToEachElement = List.map add1
  2. // "add1ToEachElement" transforms lists to lists rather than ints to ints
  3. // val add1ToEachElement : (int list -> int list)
  4. // now use it
  5. [1..5] |> add1ToEachElement
  6. // [2; 3; 4; 5; 6]

collect works to flatten lists. If you already have a list of lists, you can use collect with id to flatten them.

  1. [2..5] |> List.collect (fun x -> [x; x*x; x*x*x] )
  2. // [2; 4; 8; 3; 9; 27; 4; 16; 64; 5; 25; 125]
  3. // using "id" with collect
  4. let list1 = [1..3]
  5. let list2 = [4..6]
  6. [list1; list2] |> List.collect id
  7. // [1; 2; 3; 4; 5; 6]

Seq.cast

Finally, Seq.cast is useful when working with older parts of the BCL that have specialized collection classes rather than generics.

For example, the Regex library has this problem, and so the code below won’t compile because MatchCollection is not an IEnumerable<T>

  1. open System.Text.RegularExpressions
  2. let matches =
  3. let pattern = "\d\d\d"
  4. let matchCollection = Regex.Matches("123 456 789",pattern)
  5. matchCollection
  6. |> Seq.map (fun m -> m.Value) // ERROR
  7. // ERROR: The type 'MatchCollection' is not compatible with the type 'seq<'a>'
  8. |> Seq.toList

The fix is to cast MatchCollection to a Seq<Match> and then the code will work nicely:

  1. let matches =
  2. let pattern = "\d\d\d"
  3. let matchCollection = Regex.Matches("123 456 789",pattern)
  4. matchCollection
  5. |> Seq.cast<Match>
  6. |> Seq.map (fun m -> m.Value)
  7. |> Seq.toList
  8. // output = ["123"; "456"; "789"]


18. Iterating over each element

Normally, when processing a collection, we transform each element to a new value using map. But occasionally we need to process all the elements with a function which doesn’t
produce a useful value (a “unit function”).

Usage examples

The most common examples of unit functions are all about side-effects: printing to the console, updating a database, putting a message on a queue, etc.
For the examples below, I will just use printfn as my unit function.

  1. [1..3] |> List.iter (fun i -> printfn "i is %i" i)
  2. (*
  3. i is 1
  4. i is 2
  5. i is 3
  6. *)
  7. // or using partial application
  8. [1..3] |> List.iter (printfn "i is %i")
  9. // or using a for loop
  10. for i = 1 to 3 do
  11. printfn "i is %i" i
  12. // or using a for-in loop
  13. for i in [1..3] do
  14. printfn "i is %i" i

As noted above, the expression inside an iter or for-loop must return unit. In the following examples, we try to add 1 to the element, and get a compiler error:

  1. [1..3] |> List.iter (fun i -> i + 1)
  2. // ~~~
  3. // ERROR error FS0001: The type 'unit' does not match the type 'int'
  4. // a for-loop expression *must* return unit
  5. for i in [1..3] do
  6. i + 1 // ERROR
  7. // This expression should have type 'unit',
  8. // but has type 'int'. Use 'ignore' ...

If you are sure that this is not a logic bug in your code, and you want to get rid of this error, you can pipe the results into ignore:

  1. [1..3] |> List.iter (fun i -> i + 1 |> ignore)
  2. for i in [1..3] do
  3. i + 1 |> ignore


19. Threading state through an iteration

The fold function is the most basic and powerful function in the collection arsenal. All other functions (other than generators like unfold) can be written in terms of it. See the examples below.

The fold function is often called “fold left” and foldBack is often called “fold right”.

The scan function is like fold but returns the intermediate results and thus can be used to trace or monitor the iteration.

Just like the fold twins, scan is often called “scan left” and scanBack is often called “scan right”.

Finally, mapFold combines map and fold into one awesome superpower. More complicated than using map and fold separately but also more efficient.

fold examples

One way of thinking about fold is that it is like reduce but with an extra parameter for the initial state:

  1. ["a";"b";"c"] |> List.fold (+) "hello: "
  2. // "hello: abc"
  3. // "hello: " + "a" + "b" + "c"
  4. [1;2;3] |> List.fold (+) 10
  5. // 16
  6. // 10 + 1 + 2 + 3

As with reduce, fold and foldBack can give very different answers.

  1. [1;2;3;4] |> List.fold (fun state x -> (state)*10 + x) 0
  2. // state at each step
  3. 1 // 1
  4. (1)*10 + 2 // 12
  5. ((1)*10 + 2)*10 + 3 // 123
  6. (((1)*10 + 2)*10 + 3)*10 + 4 // 1234
  7. // Final result is 1234

And here’s the foldBack version:

  1. List.foldBack (fun x state -> x + 10*(state)) [1;2;3;4] 0
  2. // state at each step
  3. 4 // 4
  4. 3 + 10*(4) // 43
  5. 2 + 10*(3 + 10*(4)) // 432
  6. 1 + 10*(2 + 10*(3 + 10*(4))) // 4321
  7. // Final result is 4321

Note that foldBack has a different parameter order to fold: the list is second last, and the initial state is last, which means that piping is not as convenient.

Recursing vs iterating

It’s easy to get confused between fold vs. foldBack. I find it helpful to think of fold as being about iteration while foldBack is about recursion.

Let’s say we want to calculate the sum of a list. The iterative way would be to use a for-loop.
You start with a (mutable) accumulator and thread it through each iteration, updating it as you go.

  1. let iterativeSum list =
  2. let mutable total = 0
  3. for e in list do
  4. total <- total + e
  5. total // return sum

On the other hand, the recursive approach says that
if the list has a head and tail, calculate the sum of the tail (a smaller list) first, and then add the head to it.

Each time the tail gets smaller and smaller until it is empty, at which point you’re done.

  1. let rec recursiveSum list =
  2. match list with
  3. | [] ->
  4. 0
  5. | head::tail ->
  6. head + (recursiveSum tail)

Which approach is better?

For aggregation, the iterative way is (fold) often easiest to understand.
But for things like constructing new lists, the recursive way is (foldBack) is easier to understand.

For example, if we were going to going to create a function from scratch that turned each element into the corresponding string,
we might write something like this:

  1. let rec mapToString list =
  2. match list with
  3. | [] ->
  4. []
  5. | head::tail ->
  6. head.ToString() :: (mapToString tail)
  7. [1..3] |> mapToString
  8. // ["1"; "2"; "3"]

Using foldBack we can transfer that same logic “as is”:

  • action for empty list = []
  • action for non-empty list = head.ToString() :: state

Here is the resulting function:

  1. let foldToString list =
  2. let folder head state =
  3. head.ToString() :: state
  4. List.foldBack folder list []
  5. [1..3] |> foldToString
  6. // ["1"; "2"; "3"]

On the other hand, a big advantage of fold is that it is easier to use “inline” because it plays better with piping.

Luckily, you can use fold (for list construction at least) just like foldBack as long as you reverse the list at the end.

  1. // inline version of "foldToString"
  2. [1..3]
  3. |> List.fold (fun state head -> head.ToString() :: state) []
  4. |> List.rev
  5. // ["1"; "2"; "3"]

Using fold to implement other functions

As I mentioned above, fold is the core function for operating on lists and can emulate most other functions,
although perhaps not as efficiently as a custom implementation.

For example, here is map implemented using fold:

  1. /// map a function "f" over all elements
  2. let myMap f list =
  3. // helper function
  4. let folder state head =
  5. f head :: state
  6. // main flow
  7. list
  8. |> List.fold folder []
  9. |> List.rev
  10. [1..3] |> myMap (fun x -> x + 2)
  11. // [3; 4; 5]

And here is filter implemented using fold:

  1. /// return a new list of elements for which "pred" is true
  2. let myFilter pred list =
  3. // helper function
  4. let folder state head =
  5. if pred head then
  6. head :: state
  7. else
  8. state
  9. // main flow
  10. list
  11. |> List.fold folder []
  12. |> List.rev
  13. let isOdd n = (n%2=1)
  14. [1..5] |> myFilter isOdd
  15. // [1; 3; 5]

And of course, you can emulate the other functions in a similar way.

scan examples

Earlier, I showed an example of the intermediate steps of fold:

  1. [1;2;3;4] |> List.fold (fun state x -> (state)*10 + x) 0
  2. // state at each step
  3. 1 // 1
  4. (1)*10 + 2 // 12
  5. ((1)*10 + 2)*10 + 3 // 123
  6. (((1)*10 + 2)*10 + 3)*10 + 4 // 1234
  7. // Final result is 1234

For that example, I had to manually calculate the intermediate states,

Well, if I had used scan, I would have got those intermediate states for free!

  1. [1;2;3;4] |> List.scan (fun state x -> (state)*10 + x) 0
  2. // accumulates from left ===> [0; 1; 12; 123; 1234]

scanBack works the same way, but backwards of course:

  1. List.scanBack (fun x state -> (state)*10 + x) [1;2;3;4] 0
  2. // [4321; 432; 43; 4; 0] <=== accumulates from right

Just as with foldBack the parameter order for “scan right” is inverted compared with “scan left”.

Truncating a string with scan

Here’s an example where scan is useful. Say that you have a news site, and you need to make sure headlines fit into 50 chars.

You could just truncate the string at 50, but that would look ugly. Instead you want to have the truncation end at a word boundary.

Here’s one way of doing it using scan:

  • Split the headline into words.
  • Use scan to concat the words back together, generating a list of fragments, each with an extra word added.
  • Get the longest fragment under 50 chars.
  1. // start by splitting the text into words
  2. let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor."
  3. let words = text.Split(' ')
  4. // [|"Lorem"; "ipsum"; "dolor"; "sit"; ... ]
  5. // accumulate a series of fragments
  6. let fragments = words |> Seq.scan (fun frag word -> frag + " " + word) ""
  7. (*
  8. " Lorem"
  9. " Lorem ipsum"
  10. " Lorem ipsum dolor"
  11. " Lorem ipsum dolor sit"
  12. " Lorem ipsum dolor sit amet,"
  13. etc
  14. *)
  15. // get the longest fragment under 50
  16. let longestFragUnder50 =
  17. fragments
  18. |> Seq.takeWhile (fun s -> s.Length <= 50)
  19. |> Seq.last
  20. // trim off the first blank
  21. let longestFragUnder50Trimmed =
  22. longestFragUnder50 |> (fun s -> s.[1..])
  23. // The result is:
  24. // "Lorem ipsum dolor sit amet, consectetur"

Note that I’m using Seq.scan rather than Array.scan. This does a lazy scan and avoids having to create fragments that are not needed.

Finally, here is the complete logic as a utility function:

  1. // the whole thing as a function
  2. let truncText max (text:string) =
  3. if text.Length <= max then
  4. text
  5. else
  6. text.Split(' ')
  7. |> Seq.scan (fun frag word -> frag + " " + word) ""
  8. |> Seq.takeWhile (fun s -> s.Length <= max-3)
  9. |> Seq.last
  10. |> (fun s -> s.[1..] + "...")
  11. "a small headline" |> truncText 50
  12. // "a small headline"
  13. text |> truncText 50
  14. // "Lorem ipsum dolor sit amet, consectetur..."

Yes, I know that there is a more efficient implementation than this, but I hope that this little example shows off the power of scan.

mapFold examples

The mapFold function can do a map and a fold in one step, which can be convenient on occasion.

Here’s an example of combining an addition and a sum in one step using mapFold:

  1. let add1 x = x + 1
  2. // add1 using map
  3. [1..5] |> List.map (add1)
  4. // Result => [2; 3; 4; 5; 6]
  5. // sum using fold
  6. [1..5] |> List.fold (fun state x -> state + x) 0
  7. // Result => 15
  8. // map and sum using mapFold
  9. [1..5] |> List.mapFold (fun state x -> add1 x, (state + x)) 0
  10. // Result => ([2; 3; 4; 5; 6], 15)


20. Working with the index of each element

Often, you need the index of the element as you do an iteration. You could use a mutable counter, but why not sit back and let the library do the work for you?

Usage examples

  1. ['a'..'c'] |> List.mapi (fun index ch -> sprintf "the %ith element is '%c'" index ch)
  2. // ["the 0th element is 'a'"; "the 1th element is 'b'"; "the 2th element is 'c'"]
  3. // with partial application
  4. ['a'..'c'] |> List.mapi (sprintf "the %ith element is '%c'")
  5. // ["the 0th element is 'a'"; "the 1th element is 'b'"; "the 2th element is 'c'"]
  6. ['a'..'c'] |> List.iteri (printfn "the %ith element is '%c'")
  7. (*
  8. the 0th element is 'a'
  9. the 1th element is 'b'
  10. the 2th element is 'c'
  11. *)

indexed generates a tuple with the index — a shortcut for a specific use of mapi:

  1. ['a'..'c'] |> List.mapi (fun index ch -> (index, ch) )
  2. // [(0, 'a'); (1, 'b'); (2, 'c')]
  3. // "indexed" is a shorter version of above
  4. ['a'..'c'] |> List.indexed
  5. // [(0, 'a'); (1, 'b'); (2, 'c')]


21. Transforming the whole collection to a different collection type

You often need to convert from one kind of collection to another. These functions do this.

The ofXXX functions are used to convert from XXX to the module type. For example, List.ofArray will turn an array into a list.

The toXXX are used to convert from the module type to the type XXX. For example, List.toArray will turn an list into an array.

Usage examples

  1. [1..5] |> List.toArray // [|1; 2; 3; 4; 5|]
  2. [1..5] |> Array.ofList // [|1; 2; 3; 4; 5|]
  3. // etc

Using sequences with disposables

One important use of these conversion functions is to convert a lazy enumeration (seq) to a fully evaluated collection such as list. This is particularly
important when there is a disposable resource involved, such as file handle or database connection. If the sequence is not converted into a list
you may encounter errors accessing the elements. See section 28 for more.


22. Changing the behavior of the collection as a whole

There are some special functions (for Seq only) that change the behavior of the collection as a whole.

  • (Seq only) cache: source:seq<'T> -> seq<'T>.
    Returns a sequence that corresponds to a cached version of the input sequence. This result sequence will have the same elements as the input sequence. The result
    can be enumerated multiple times. The input sequence will be enumerated at most once and only as far as is necessary.
  • (Seq only) readonly : source:seq<'T> -> seq<'T>.
    Builds a new sequence object that delegates to the given sequence object. This ensures the original sequence cannot be rediscovered and mutated by a type cast.
  • (Seq only) delay : generator:(unit -> seq<'T>) -> seq<'T>.
    Returns a sequence that is built from the given delayed specification of a sequence.

cache example

Here’s an example of cache in use:

  1. let uncachedSeq = seq {
  2. for i = 1 to 3 do
  3. printfn "Calculating %i" i
  4. yield i
  5. }
  6. // iterate twice
  7. uncachedSeq |> Seq.iter ignore
  8. uncachedSeq |> Seq.iter ignore

The result of iterating over the sequence twice is as you would expect:

  1. Calculating 1
  2. Calculating 2
  3. Calculating 3
  4. Calculating 1
  5. Calculating 2
  6. Calculating 3

But if we cache the sequence…

  1. let cachedSeq = uncachedSeq |> Seq.cache
  2. // iterate twice
  3. cachedSeq |> Seq.iter ignore
  4. cachedSeq |> Seq.iter ignore

… then each item is only printed once:

  1. Calculating 1
  2. Calculating 2
  3. Calculating 3

readonly example

Here’s an example of readonly being used to hide the underlying type of the sequence:

  1. // print the underlying type of the sequence
  2. let printUnderlyingType (s:seq<_>) =
  3. let typeName = s.GetType().Name
  4. printfn "%s" typeName
  5. [|1;2;3|] |> printUnderlyingType
  6. // Int32[]
  7. [|1;2;3|] |> Seq.readonly |> printUnderlyingType
  8. // mkSeq@589 // a temporary type

delay example

Here’s an example of delay.

  1. let makeNumbers max =
  2. [ for i = 1 to max do
  3. printfn "Evaluating %d." i
  4. yield i ]
  5. let eagerList =
  6. printfn "Started creating eagerList"
  7. let list = makeNumbers 5
  8. printfn "Finished creating eagerList"
  9. list
  10. let delayedSeq =
  11. printfn "Started creating delayedSeq"
  12. let list = Seq.delay (fun () -> makeNumbers 5 |> Seq.ofList)
  13. printfn "Finished creating delayedSeq"
  14. list

If we run the code above, we find that just by creating eagerList, we print all the “Evaluating” messages. But creating delayedSeq does not trigger the list iteration.

  1. Started creating eagerList
  2. Evaluating 1.
  3. Evaluating 2.
  4. Evaluating 3.
  5. Evaluating 4.
  6. Evaluating 5.
  7. Finished creating eagerList
  8. Started creating delayedSeq
  9. Finished creating delayedSeq

Only when the sequence is iterated over does the list creation happen:

  1. eagerList |> Seq.take 3 // list already created
  2. delayedSeq |> Seq.take 3 // list creation triggered

An alternative to using delay is just to embed the list in a seq like this:

  1. let embeddedList = seq {
  2. printfn "Started creating embeddedList"
  3. yield! makeNumbers 5
  4. printfn "Finished creating embeddedList"
  5. }

As with delayedSeq, the makeNumbers function will not be called until the sequence is iterated over.


23. Working with two lists

If you have two lists, there are analogues of most of the common functions like map and fold.

Usage examples

These functions are straightforward to use:

  1. let intList1 = [2;3;4]
  2. let intList2 = [5;6;7]
  3. List.map2 (fun i1 i2 -> i1 + i2) intList1 intList2
  4. // [7; 9; 11]
  5. // TIP use the ||> operator to pipe a tuple as two arguments
  6. (intList1,intList2) ||> List.map2 (fun i1 i2 -> i1 + i2)
  7. // [7; 9; 11]
  8. (intList1,intList2) ||> List.mapi2 (fun index i1 i2 -> index,i1 + i2)
  9. // [(0, 7); (1, 9); (2, 11)]
  10. (intList1,intList2) ||> List.iter2 (printf "i1=%i i2=%i; ")
  11. // i1=2 i2=5; i1=3 i2=6; i1=4 i2=7;
  12. (intList1,intList2) ||> List.iteri2 (printf "index=%i i1=%i i2=%i; ")
  13. // index=0 i1=2 i2=5; index=1 i1=3 i2=6; index=2 i1=4 i2=7;
  14. (intList1,intList2) ||> List.forall2 (fun i1 i2 -> i1 < i2)
  15. // true
  16. (intList1,intList2) ||> List.exists2 (fun i1 i2 -> i1+10 > i2)
  17. // true
  18. (intList1,intList2) ||> List.fold2 (fun state i1 i2 -> (10*state) + i1 + i2) 0
  19. // 801 = 234 + 567
  20. List.foldBack2 (fun i1 i2 state -> i1 + i2 + (10*state)) intList1 intList2 0
  21. // 1197 = 432 + 765
  22. (intList1,intList2) ||> List.compareWith (fun i1 i2 -> i1.CompareTo(i2))
  23. // -1
  24. (intList1,intList2) ||> List.append
  25. // [2; 3; 4; 5; 6; 7]
  26. [intList1;intList2] |> List.concat
  27. // [2; 3; 4; 5; 6; 7]
  28. (intList1,intList2) ||> List.zip
  29. // [(2, 5); (3, 6); (4, 7)]

Need a function that’s not here?

By using fold2 and foldBack2 you can easily create your own functions. For example, some filter2 functions can be defined like this:

  1. /// Apply a function to each element in a pair
  2. /// If either result passes, include that pair in the result
  3. let filterOr2 filterPredicate list1 list2 =
  4. let pass e = filterPredicate e
  5. let folder e1 e2 state =
  6. if (pass e1) || (pass e2) then
  7. (e1,e2)::state
  8. else
  9. state
  10. List.foldBack2 folder list1 list2 ([])
  11. /// Apply a function to each element in a pair
  12. /// Only if both results pass, include that pair in the result
  13. let filterAnd2 filterPredicate list1 list2 =
  14. let pass e = filterPredicate e
  15. let folder e1 e2 state =
  16. if (pass e1) && (pass e2) then
  17. (e1,e2)::state
  18. else
  19. state
  20. List.foldBack2 folder list1 list2 []
  21. // test it
  22. let startsWithA (s:string) = (s.[0] = 'A')
  23. let strList1 = ["A1"; "A3"]
  24. let strList2 = ["A2"; "B1"]
  25. (strList1, strList2) ||> filterOr2 startsWithA
  26. // [("A1", "A2"); ("A3", "B1")]
  27. (strList1, strList2) ||> filterAnd2 startsWithA
  28. // [("A1", "A2")]

See also section 25.


24. Working with three lists

If you have three lists, you only have one built-in function available. But see section 25 for an example of how you can build your own three-list functions.


25. Working with more than three lists

If you are working with more than three lists, there are no built in functions for you.

If this happens infrequently, then you could just collapse the lists into a single tuple using zip2 and/or zip3 in succession, and then process that tuple using map.

Alternatively you can “lift” your function to the world of “zip lists” using applicatives.

  1. let (<*>) fList xList =
  2. List.map2 (fun f x -> f x) fList xList
  3. let (<!>) = List.map
  4. let addFourParams x y z w =
  5. x + y + z + w
  6. // lift "addFourParams" to List world and pass lists as parameters rather than ints
  7. addFourParams <!> [1;2;3] <*> [1;2;3] <*> [1;2;3] <*> [1;2;3]
  8. // Result = [4; 8; 12]

If that seems like magic, see this series for a explanation of what this code is doing.


26. Combining and uncombining collections

Finally, there are a number of functions that combine and uncombine collections.

Usage examples

These functions are straightforward to use:

  1. List.append [1;2;3] [4;5;6]
  2. // [1; 2; 3; 4; 5; 6]
  3. [1;2;3] @ [4;5;6]
  4. // [1; 2; 3; 4; 5; 6]
  5. List.concat [ [1]; [2;3]; [4;5;6] ]
  6. // [1; 2; 3; 4; 5; 6]
  7. List.zip [1;2] [10;20]
  8. // [(1, 10); (2, 20)]
  9. List.zip3 [1;2] [10;20] [100;200]
  10. // [(1, 10, 100); (2, 20, 200)]
  11. List.unzip [(1, 10); (2, 20)]
  12. // ([1; 2], [10; 20])
  13. List.unzip3 [(1, 10, 100); (2, 20, 200)]
  14. // ([1; 2], [10; 20], [100; 200])

Note that the zip functions require the lengths to be the same.

  1. List.zip [1;2] [10]
  2. // ArgumentException: The lists had different lengths.


27. Other array-only functions

Arrays are mutable, and therefore have some functions that are not applicable to lists and sequences.

  • See the “sort in place” functions in section 15
  • Array.blit: source:'T[] -> sourceIndex:int -> target:'T[] -> targetIndex:int -> count:int -> unit.
    Reads a range of elements from the first array and write them into the second.
  • Array.copy: array:'T[] -> 'T[].
    Builds a new array that contains the elements of the given array.
  • Array.fill: target:'T[] -> targetIndex:int -> count:int -> value:'T -> unit.
    Fills a range of elements of the array with the given value.
  • Array.set: array:'T[] -> index:int -> value:'T -> unit.
    Sets an element of an array.
  • In addition to these, all the other BCL array functions are available as well.

I won’t give examples. See the MSDN documentation.


28. Using sequences with disposables

One important use of conversion functions like List.ofSeq is to convert a lazy enumeration (seq) to a fully evaluated collection such as list. This is particularly
important when there is a disposable resource involved such as file handle or database connection. If the sequence is not converted into a list
while the resource is available you may encounter errors accessing the elements later, after the resource has been disposed.

This will be an extended example, so let’s start with some helper functions that emulate a database and a UI:

  1. // a disposable database connection
  2. let DbConnection() =
  3. printfn "Opening connection"
  4. { new System.IDisposable with
  5. member this.Dispose() =
  6. printfn "Disposing connection" }
  7. // read some records from the database
  8. let readNCustomersFromDb dbConnection n =
  9. let makeCustomer i =
  10. sprintf "Customer %i" i
  11. seq {
  12. for i = 1 to n do
  13. let customer = makeCustomer i
  14. printfn "Loading %s from db" customer
  15. yield customer
  16. }
  17. // show some records on the screen
  18. let showCustomersinUI customers =
  19. customers |> Seq.iter (printfn "Showing %s in UI")

A naive implementation will cause the sequence to be evaluated after the connection is closed:

  1. let readCustomersFromDb() =
  2. use dbConnection = DbConnection()
  3. let results = readNCustomersFromDb dbConnection 2
  4. results
  5. let customers = readCustomersFromDb()
  6. customers |> showCustomersinUI

The output is below. You can see that the connection is closed and only then is the sequence evaluated.

  1. Opening connection
  2. Disposing connection
  3. Loading Customer 1 from db // error! connection closed!
  4. Showing Customer 1 in UI
  5. Loading Customer 2 from db
  6. Showing Customer 2 in UI

A better implementation will convert the sequence to a list while the connection is open, causing the sequence to be evaluated immediately:

  1. let readCustomersFromDb() =
  2. use dbConnection = DbConnection()
  3. let results = readNCustomersFromDb dbConnection 2
  4. results |> List.ofSeq
  5. // Convert to list while connection is open
  6. let customers = readCustomersFromDb()
  7. customers |> showCustomersinUI

The result is much better. All the records are loaded before the connection is disposed:

  1. Opening connection
  2. Loading Customer 1 from db
  3. Loading Customer 2 from db
  4. Disposing connection
  5. Showing Customer 1 in UI
  6. Showing Customer 2 in UI

A third alternative is to embed the disposable in the sequence itself:

  1. let readCustomersFromDb() =
  2. seq {
  3. // put disposable inside the sequence
  4. use dbConnection = DbConnection()
  5. yield! readNCustomersFromDb dbConnection 2
  6. }
  7. let customers = readCustomersFromDb()
  8. customers |> showCustomersinUI

The output shows that now the UI display is also done while the connection is open:

  1. Opening connection
  2. Loading Customer 1 from db
  3. Showing Customer 1 in UI
  4. Loading Customer 2 from db
  5. Showing Customer 2 in UI
  6. Disposing connection

This may be a bad thing (longer time for the connection to stay open) or a good thing (minimal memory use), depending on the context.


29. The end of the adventure

You made it to the end — well done! Not really much of an adventure, though, was it? No dragons or anything. Nevertheless, I hope it was helpful.