liveBook · Manning

Sequence

As explained in F# in Action: A Revised Edition of Get Programming with F#

A sequence is an ordered, potentially lazy and forward-only series of elements, represented in .NET by the IEnumerable interface.

A sequence is a lazy, forward-only series of values represented by F#'s `seq` type, an alias for `System.Collections.Generic.IEnumerable<T>`. Unlike an array or list, it can describe a computation that produces values only when they are requested rather than storing a fixed contiguous block of data in memory. section 7.3.2 section 7.3.7

Arrays and lists can both be treated as sequences because they implement `IEnumerable`. Sequences are useful when compatibility with .NET code or lazy generation matters, but they are less predictable than lists: they have no indexer, and accessing an element uses functions such as `Seq.item`. section 7.3.2 section 7.3.9

Listing 7.6 Creating and consuming a Sequence
let numbers = seq { 1 .. 10 }                       #1
let secondElement = numbers |> Seq.item 1           #2
let squares = numbers |> Seq.map (fun x -> x * x)   #3

Create a sequence with `seq { ... }`, then consume it through `Seq` functions. Mapping squares describes the transformation without immediately producing the squared values. section 7.3.2

Treat a sequence as a generator, and account for its execution behavior:

  • Expect work to be deferred. A mapped sequence remains a sequence until a consumer evaluates it. section 7.3.7
  • Expect repeated evaluation. Reading the same sequence again can rerun its entire pipeline. section 7.3.7
  • Materialize or cache results when repeated access to expensive data is needed, using an array, list, or `Seq.cache`. section 7.3.7
  • Use sequences for potentially unbounded streams, where eagerly building every value would not be practical. section 7.3.7

Generate and filter dates

A comprehension can turn imperative-style date progression into a `DateTime seq`. Filter that stream for Mondays, skip dates before February 2020, stop before June, and finally convert the required results to an array. The same pattern can model console input: generate characters as they arrive, pass them to a command processor, and test that processor with a fixed character list. section 7.4.3 section 7.4.3

Comprehensions can include expressions, conditions, nested logic, and other calls, letting a sequence be assembled programmatically. They can also expose an imperative source as data that ordinary `Seq` operations can map, filter, fold, or otherwise compose into a pipeline. section 7.3.8 section 7.6

This uniform treatment can decouple processing from where values originate. A pipeline over a `string seq`, for example, can operate on console input and could instead receive a text file or database data, provided the input has the same sequence shape. Event streams can also be handled as sequences with built-in functions and third-party libraries. section 10.1.1 section 10.1.1

For APIs consumed from C#, prefer arrays or sequences over F# lists. A sequence is already `IEnumerable`, a familiar .NET abstraction that works with LINQ and is commonly expected by C# developers. section 11.2.2

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains deferred execution, recomputation, caching, comprehensions, and practical sequence pipelines without detailing IEnumerable internals.
Examples
Uses number ranges and squares, conditional comprehensions, date streams, console commands, and timer events.
What it assumes
Introduces sequences as a collection form while assuming enough F# to read pipelines, functions, and basic collection code.
What you can do after
Apply it via `seq` and `Seq` functions to build lazy pipelines, then cache or materialize costly results deliberately.
How it connects
Connects `IEnumerable`, List, Array, `Seq` functions, comprehensions, LINQ, event streams, and functional pipelines.

Also explained in

Compare how 22 books explain sequence →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.