Creating sequences from collections

Any collection that implements IEnumerable is fairly easy to convert to a sequence. This is because, by definition, IEnumerable is a sequence in F#. Since strings and arrays implement IEnumerable, they can be processed without any explicit conversion into functions by using the casting operators on a sequence as seen next:

let seqFromArray = [| 1 .. 10 |] :> seq<int>
let seqFromArray = [| 1 .. 10 |] |> Seq.ofArray

Note

The challenge here might be in a case when such functions return a sequence; you may need to convert the returned elements back into an array using Array.ofSeq or Seq.toArray.

You also see the type casting operators in the preceding statements. The operator :> converts a type to another type that ...

Get Learning F# Functional Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.