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

Enumerating a CSV file


As you probably have noticed by now, a sequence is quite a versatile data structure and we can use a file to populate a sequence. As seen in the next screenshot, I have created a tab-separated text file with the information about some programming languages, and their respective designers or creators:

Like various other methods specified earlier for populating a sequence, it can also easily be seeded by reading from the text file as follows:

let data = seq { use s = new System.IO.StreamReader("ProgrammingLanguages.txt")
  while not s.EndOfStream do yield s.ReadLine() }

And just like any other sequence, you can print the data by using the printfn method:

> data |> printfn "%A";;
seq
  ["C	Dennis MacAlistair Ritchie	1972"; "C++	Bjarne Stroustrup	1985";
   "C#	Anders Hejlsberg	2000"; "F#	Don Syme	2005"; ...]
val it : unit = ()
>

OK, now that we have this basic functionality out of our system, let's build something useful using sequences such as an XSV enumerator that...