Lunar Programming Language

by David A. Moon
January 2017 - January 2018



Displays and Comprehensions

Displays and comprehensions are convenient syntax for constructing collections. You can construct constant lists, sets, and maps with default generic parameters.

List Displays

A list display is an expression that constructs a constant_list from a series of expressions. The result of each expression is one member. The expressions are separated by commas and enclosed in square brackets.

For example, the result of

[2 + 2, 3 - 1, 5 * "x"]
is the list
[4, 2, "xxxxx"]
This is implemented by the [ prefix macro.

To construct a variable list from a list display

list!([2 + 2, 3 - 1, 5 * "x"]...)

To construct a list of restricted member type from a list display

list[integer]([2 + 2, 3 - 1, 5]...)

List Comprehensions

A list comprehension is like a list display but the series of expressions is followed by a for clause which consists of the particle for, one or more emitters ( see For Statement ), zero or more end tests, and zero or more additional for and/or if clauses. The various clauses are nested from left to right. For each iteration where any if clauses are satisfied, the series of expressions are executed. The resulting lists are concatenated together to produce the result of the comprehension. For example, the result of

[ i, j for i in 10..12 for j in i..i+2 ]
is the list
[10, 10, 10, 11, 10, 12, 11, 11, 11, 12, 11, 13, 12, 12, 12, 13, 12, 14]
while the result of
[ [ i, j for j in i..i+2 ] for i in 10..12 ]
is the list
[ [10, 10, 10, 11, 10, 12], [11, 11, 11, 12, 11, 13], [12, 12, 12, 13, 12, 14] ]
and the result of
[ i, j for i, j in 10..16 ]
is the list
[10, 11, 12, 13, 14, 15]

This too is implemented by the [ prefix macro.

Set Displays

A set display is an expression that constructs a constant_set from a series of expressions. The result of each expression is one member, assuming no duplicates. The expressions are separated by commas and enclosed in curly brackets.

For example, the result of

{2 + 2, 3 - 1, 5 * "x"}
is the set
{4, 2, "xxxxx"}
not necessarily in that order.

This is implemented by the { prefix macro.

Set Comprehensions

A set comprehension is just like a list comprehension, but is enclosed in curly brackets and produces a set.

For example, the result of

{x for x in 1..10 if even?(x)}
is the set
{2, 4, 6, 8, 10}
not necessarily in that order.

Map Displays

A map display is an expression that constructs a constant_map from a series of key/member pairs. Each pair produces one member, assuming no duplicate keys. The pairs are separated by commas and enclosed in curly brackets.

Each pair is either key_expression => member_expression or keyword member_expression. When a keyword is used, the key is the name of the keyword, otherwise the key is the result of the key_expression.

For example, the result of

def a = 1
def b = 2
def c = 3
{x:a, y:b, z:c}
is the map
{#x => 1, #y => 2, #z => 3}
not necessarily in that order.

The display could also be written as

{#x => a, #y => b, #z => c}

This too is implemented by the { prefix macro.

Map Comprehensions

A map comprehension is just like a list comprehension, but is enclosed in curly brackets, contains key/member pairs, and produces a map.

For example, the result of

{x => y for x, y in 1..10}
is the map
{1 => 2, 3 => 4, 5 => 6, 7 => 8, 9 => 10}
not necessarily in that order.


Previous page   Table of Contents   Next page



Creative Commons License
Lunar by David A. Moon is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Please inform me if you find this useful, or use any of the ideas embedded in it.
Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.