Lazy loaded sequences in F#

F# has a number of standard collection types that are available in other popular computing languages: lists, arrays, tuples. There’s, however, an odd one called a sequence. A sequence in F# is a normal collection for IEnumerable types but its members are lazily evaluated. This means that the sequence is not instantiated with all its elements included. Instead, its elements are evaluated when needed.

Here’s how we can declare a sequence:

let myFirstSequence = seq {0..10}

So we use curly braces and the seq keyword. Sequences can be initiated with the same range expressions as in the case of lists:

seq {0..2..100}
seq {for i in 1 .. 100 do yield i * 2}
seq {for i in 1 .. 100 -> i * 2}
seq {for n in 1 .. 100 do if n % 2 = 0 then yield n}

At this point the sequence logically includes all these elements but they haven’t been initialised in memory. Sequences are most useful when dealing with large collections of data where not all elements are needed in the application. E.g. if we always pull out the first or last element from the collection then a sequence is a good candidate.

Sequences also come with a large number of functions available through the Seq module. Just type “Seq.” in Visual Studio and it will give you a long list of functions that can be applied to sequences. Here’s an example of using the map function:

let myFirstSequence = seq {1..10}
let mapped = myFirstSequence |> Seq.map (fun i -> if i > 5 then "more than 5" else if i = 5 then "is 5" else "less than five")
for x in mapped do
    printfn "%s" x

The above code outputs the following:

less than five
less than five
less than five
less than five
is 5
more than 5
more than 5
more than 5
more than 5
more than 5

View all F# related articles here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: