Queries - Spicery/Nutmeg GitHub Wiki
Introduction
In Nutmeg, queries are a powerful generalisation of the simple idea of a binding. A binding such as x := 99
has one and only one solution, with x
being bound to the constant 99
. By contrast, queries can have multiple solutions. For example:
i in "abcde"
has 5 solutions, where i
is bound to each of the characters of "abcde" in turn. To iterate over all the possible solutions of a query, we use a for
loop.
for i in "abcde" do
STATEMENTS
endfor
Hence the above code executes the STATEMENTS 5 times and, on each execution, i
is bound to a different character.
Early Exit
It is possible to truncate the solutions of a query by using the modifiers while
or until
. In this example, we stop the iteration as soon as we get to the letter g
.
for i in "abcdefghiklm" until i == `g`:
println( i )
endfor
Both while
and until
are forms of early exit from solution-exploration. And we often want to take different action depending on how we exit. We can use the then
clause to specify what to do on an early exit and the afterwards
clause to say what to do if we do not exit early:
def has_x( s ):
for
ch in s afterwards false
until ch == `x` then true:
endfor
enddef
Note that the afterwards
clause modifies the main query ch in a
. Consequently only gets executed if ch in s
manages to complete. The until
clause modifies ch in s afterwards false
and, if it finds an x
, will cut off all the remainder, including the afterwards
action.