Categories

# F# Friday – The Seq.take and Seq.truncate Functions

Sometimes you have a collection of items, and you only want the first so many items. You may not know or even care what the first n items are, you just know that you want the first n items. `Seq.take` can do that for you.

### To Infinity … and Beyond!

OK, it’s unusual that you just don’t care what items you get from a sequence, but there are occasions: for instance, random numbers. You really don’t care what the number is. You just don’t want it to be the same number every time, and perhaps you want it to fall within a certain range. Other than that, you don’t care: you just want a number!

```let seed = System.DateTime.UtcNow.GetHashCode()
let rand = System.Random(seed)
// val someNum : int = 689587617  (well, this time anyway)
```

Now, assume that you have an application that needs a steady stream of random numbers. (Maybe even the number of random numbers you want is itself random!) Wouldn’t it be great to turn your random number into a sequence?

F# sequences are lazy data structures. That means that as you iterate over the sequence, it produces the next item on demand. It may not even have calculated what the next item is until you ask for it. In fact, sequences can be infinite! How do you have an infinite sequence? Well, back to the random number generator.

You can call `rand.Next()` until the cows come home and just keep getting values. Well, the `Seq` module gives us a way to turn `rand` (or any function that calculates a value from an input) into an infinite sequence—`Seq.initInfinite`:

```let rands = Seq.initInfinite (fun _ -> rand.Next())
```

`Seq.initInfinite` takes a function that takes an integer and returns a value of the type you are interested in—an integer in this case. Now you can call `Seq.take` on `rands`, and get as many or as few values as you may want:

```let taken = rands |> Seq.take 5
// val taken :
//   seq [
//     974631199
//     1778702212
//     1311642930
//     1549454908
//     1138487539
//   ]
```

Additionally, F#’s sequence expressions allow you to create the infinite sequence of random numbers in what may be a slightly more readable fashion:

```let rands = seq { while true do yield rand.Next() }
let taken = rands |> Seq.take 5
// val taken :
//   seq [
//     1882861480
//     469452280
//     466017706
//     417690089
//     166247112
//   ]
```

Use whichever approach you find the more natural.

### Going for the Gold

There are, of course, times when your data set is not infinite. You happen not to need all of them, but only the first few. I mentioned early on how that you may not know or care what the items are. A better way of saying it is that you (or someone) have likely already done the work to prepare the data set—processed it, sorted it, whatever. Now that you have done all that preprocessing, you want the first few items.

Perhaps you are writing software to determine the gold, silver, and bronze medal winners in the Olympic Games. You have a type representing each competitor:

```type Competitor = {
Name : string
Score : float
}
```

You have the list of competitors for the 2012 men’s 110-meter hurdles, and they are already sorted according to ranking:

```let competitors =
seq [
{ Name = "Aries MERRITT"; Score = 12.92 }
{ Name = "Jason RICHARDSON"; Score = 13.04 }
{ Name = "Hansle PARCHMENT"; Score = 13.12 }
{ Name = "Lawrence CLARKE"; Score = 13.39 }
{ Name = "Ryan BRATHWAITE"; Score = 13.40 }
{ Name = "Orlando ORTEGA"; Score = 13.43 }
{ Name = "Lehann FOURIE"; Score = 13.53 }
]
```

Now to get the medalists, take the first three records:

```let medalists = competitors |> Seq.take 3
// val medalists: seq<Competitor> =
//   [
//     {Name = "Aries MERRITT"; Score = 12.92;}
//     {Name = "Jason RICHARDSON"; Score = 13.04;}
//     {Name = "Hansle PARCHMENT"; Score = 13.12;}
//   ]
```

### Take It to the Limit

What happens, though, when you have a sequence with fewer items than you attempt to take?

```let numbersOfTheCounting = seq [1;2;3] |> Seq.take 5
numbersOfTheCounting |> Seq.iter (printfn "%A")
// 1
// 2
// 3
// System.InvalidOperationException:
//   The input sequence has an insufficient
//   number of elements.
```

Oops! That’s not good! If you ask for more items than the sequence contains, you get an exception. I have to admit that I find this annoying. To me, `Seq.take` ought to return as many items as it can. If it should happen to contain fewer than you’ve asked for, oh well, no big deal; here are the ones it’s got. As a point of comparison, the `take` functions in Scala, Clojure, and Haskell all behave that way.

The good news is that there is an F# function that does behave that way: `Seq.truncate`. So if you want to use a function that’s a little more forgiving, just use `Seq.truncate`:

```let numbersOfTheCounting = seq [1;2;3] |> Seq.truncate 5
numbersOfTheCounting |> Seq.iter (printfn "%A")
// 1
// 2
// 3
```

As of F# 4.0, there are versions of `take` and `truncate` in the `Array` and `List` modules, but as of this writing, the documentation on MSDN does not yet include them.

This site uses Akismet to reduce spam. Learn how your comment data is processed.