Chapter 3¶
Sequences¶
Given a sequence of values, we can evaluate a block of code for each value in the sequence. There are a number of different sequence types in Minilang including lists, maps, sets, and more. Minilang also provides methods for forming new sequences from existing sequences by transforming or filtering values. This will described in more detail later, for now we will consider ranges, lists and maps.
Ranges¶
They are several types of ranges in Minilang, in general they all have a starting value and either an ending value or number of steps. Numeric ranges can optionally have a step size, if omitted this defaults to 1.
Lists¶
In Minilang, a list is simply a list of values, of any type. List are written between [ and ]. Lists can also be created using the list() constructor which takes a sequence and collects its values into a list.
Note
In Minilang, most methods that reorder the elements of a list (sorting, reversing, etc), are in-place, i.e. they modify the list itself instead of creating a new list. They also generally returned the list allowing them to be chained easily. If the original list is required unchanged, create a copy of the list first using list().
They are numerous other methods available for lists, they can be found here list.
Maps¶
A map is a collection of key-value pairs, where the keys are unique. Maps can be written between { and }, or created using the map() constructor which takes the keys and values from a sequence and collects them into a map.
Note
Sequences in Minilang are always expected to contain or generate keys and values. Usually the values of a sequence are obvious. On the other hand, although some sequences have obvious keys (e.g. maps), often the keys are implicit. The keys of numeric ranges, strings and lists are 1, 2, ....
Note
Maps return nil when indexed with a key that is not present in the map. It is possible (but not recommended) to store nil as the value in a map and to use Key in Map to distinguish between a missing key and a key associated with nil.
Strings¶
Strings in Minilang are also sequences. When used as a sequence, they generate the individual characters as values, with 1, 2, ... as keys.
for-expressions¶
It is possible to iterate over any sequence in Minilang using a for-expression. If a single variable is declared in the for-declaration, it will be assigned the values of the sequence at each iteration. If two variables are declared, then the first will be assigned the keys and the second the values of the sequence.
As mentioned before, nearly every construct in Minilang is an expression with a vaule; a for-expression evaluates to nil when it completes normally. A for-expression can be exited at any step using an exit-expression. An exit-expression can optionally take another expression, the result of which will be the result of the for-expression. It is also possible to skip to the next iteration using next.