Book Image

Learning F# Functional Data Structures and Algorithms

By : Adnan Masood
Book Image

Learning F# Functional Data Structures and Algorithms

By: Adnan Masood

Overview of this book

Table of Contents (21 chapters)
Learning F# Functional Data Structures and Algorithms
Credits
Foreword
Foreword
Foreword
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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 is higher in the hierarchy. Its counterpart, the :?> operator, converts a type to a different type that is lower in the hierarchy.