36 Lists

The fold .Σ collects reached values as a set. The list form Σlist collects reached values as an ordered list.

For example, this expression gives the course codes in code order:


u.course.Σlist(code order by code)

The path u.course reaches courses. For each course, code produces the list item, and the order by code clause says how to order the list.

List items can be tuples

The list item can be a tuple expression. The following expression gives a list of course summaries:


u.course.Σlist((code, title) order by code)

The tuple expression controls the shape of each list item. The order by clause controls the order of the list items.

Ordering keys

The ordering key does not have to be part of the list item:


u.course.Σlist(title order by code)

This lists course titles but orders them by course code.

A simple ordering key is enough when that key determines the intended order:


u.student.Σlist(name order by name)

Ordering direction can be written with ↑ for ascending and ↓ for descending. If no direction is written, ascending order is assumed. Several ordering keys may be supplied; they are compared from left to right:


u.course.Σlist((code, title) order by advanced↓, code↑)

Filtering list items

A list expression can include an if clause. Only reached values satisfying the condition contribute list items:


u.course.Σlist(code order by code if advanced)

This gives an ordered list of advanced course codes.

The filter condition can use path tails, just like an ordinary filter:


u.course.Σlist(
    (code, title)
    order by code
    if assessment.exam.Σexists
)

This lists the courses that have at least one exam assessment.

Lists and sets

Use .Σ when the result should be a set. Use Σlist when order is part of the result.


u.course.code.Σ
u.course.Σlist(code order by code)

The first expression produces a set of course codes. The second produces a list of course codes ordered by code. Set operations such as ⊆, ∈ and ∩ are for sets; list operations are for ordered values.

List items can use earlier ideas

The list item is a path expression evaluated for each reached value, so it can use tuple construction, calculations, alternatives and conditionals.


u.course.Σlist(
    (
        code,
        (if advanced then "advanced" else "introductory") as level,
        assessment.(exam.maxMark | assignment.maxMark | project.maxMark).Σsum
            as totalMaxMark
    )
    order by code
)

This produces an ordered list of course summaries. Each summary contains the course code, a calculated level, and the total maximum mark across assessments.

Composability

Because Σlist is path-based, it can be used inside tuple construction to attach an ordered list to each current value.


u.department.(
    name,
    course.Σlist((code, title) order by code) as courses
)

This reaches each department and attaches an ordered list of the courses offered by that department.