liveBook · Manning

Sequence

As explained in Fabulous Adventures in Data Structures and Algorithms

A sequence is an ordered abstraction representing elements that can be processed in a defined order.

A sequence is an ordered abstraction whose elements can be processed in a defined order. Sequences can serve as both inputs and outputs: a Cartesian product combines items from input sequences, while mapping transforms each item into a new sequence. section 6.1 section 11.2

When a Cartesian product takes an arbitrary number of sequences:

  • Its result is a sequence of sequences, with one output element drawn from each input sequence. section 6.1.2
  • Zero input sequences yield one empty sequence; one input sequence yields one-element output sequences. section 6.1.2
  • A sequence can also be transformed element by element; the example divides every integer by two to produce a sequence of doubles. section 11.2
Listing 6.5 A LINQ-based implementation of the arbitrary Cartesian product
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
    this IEnumerable<IEnumerable<T>> sequences) 
{
    IEnumerable<IDeque<T>> result = [Deque<T>.Empty];
    foreach (var sequence in sequences)
        result = from deque in result 
                 from item in sequence
                 select deque.PushRight(item);
    return result;
}

This implementation builds the product incrementally, extending each partial result with an item from the next sequence. section 6.1.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Derives an arbitrary-input Cartesian product, including zero- and one-sequence cases, and demonstrates element-wise mapping.
Examples
Demonstrates character-and-weapon combinations, products of integer sequences, and mapping division by two over integers.
What it assumes
Introduces query expressions as an alternative to nested loops and points unfamiliar readers to a separate C# primer.
What you can do after
Apply sequence-based mapping and Cartesian-product operations using LINQ query expressions.
How it connects
Connects sequences to Cartesian products, LINQ query expressions, and monadic lifting.

Also explained in

Compare how 22 books explain sequence →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.