86 Lists

The Σlist expression collects the values reached by a path into a list. Unlike Σ, which collects an unordered set, Σlist specifies the order in which the values appear.

For example, the following expression lists part names ordered by part weight:


s.part.Σlist(name order by weight)

The list item may contain several output values by using a tuple expression:


s.part.Σlist((name, weight) order by weight)

When the list item is a tuple expression, the tuple expression controls the order and names of the fields in each list item. The order by clause controls the order of the list items themselves. These are independent: a report can display fields in one order while sorting rows by another expression.

The output may use arbitrary expressions evaluated in the context of each value reached by the path:


s.part.Σlist((name, weight * 1000) order by weight)

Several ordering keys may be supplied. Keys are compared lexicographically from left to right:


s.part.Σlist(name order by weight, name)

Each key may specify a direction. ↑ means ascending and ↓ means descending. If no direction is written, ascending order is assumed:


s.part.Σlist(name order by weight↓, name↑)

A filter may be supplied after the ordering clause. Only values satisfying the filter condition contribute list items:


s.supplier.Σlist(name order by status if city = "London")

s.supplier.Σlist(
    (name, shipment.Σcount)
    order by shipment.Σcount↓, name
    if city = "London"
)

Syntax


ListExpression =
    "Σlist", "(", ListItem, [ ListOrderClause ], [ ListFilterClause ], ")";

ListItem =
    Expression;

ListOrderClause =
    "order", "by", ListOrderKey, { ",", ListOrderKey };

ListOrderKey =
    Expression, [ ListOrderDirection ];

ListOrderDirection =
      "↑"
    | "↓";

ListFilterClause =
    "if", Expression;

The zero-argument form E.Σlist is shorthand for E.Σlist(this order by this). It is valid only when the element type of E is ordered.

Meaning

Let E.Σlist(item order by k₁ d₁, ..., kₙ dₙ if c) be a list expression, where each dᵢ is either ascending or descending. The expression E is evaluated as a path expression, and each value reached by E is considered in turn as the current element. In the context of that element:

  • c is evaluated as a Boolean filter condition, if present.
  • item is evaluated to produce the list item.
  • Each kᵢ is evaluated to produce the ordering key for that item.

Values for which the filter condition is false do not contribute list items. The remaining items are ordered lexicographically by their key values. For an ascending key, smaller values appear earlier. For a descending key, larger values appear earlier. If no order by clause is written in the parenthesised form, the list items are produced without a language-defined ordering.

Every ordering key must be single-valued and ordered. The filter condition must be Boolean. The result type of the whole expression is List<T>, where T is the type of item.

Σlist is a built-in list-forming operation. It is conceptually fold-like because it consumes the extension of a path and produces a value, but its item, ordering, and filter clauses are language syntax evaluated in the element context.