General:.
:select(selector) [.fl] {
:case (target) {arg[.fl]{code}}
:default {arg[.fl]{code}}
}
Above you see the general form of the :select/ :case / :default
structure. The italicized portions are optional. :select takes a
"selector" expression. Optionally it takes a field list
specifying the context in which :select will operate. Taking each next
:case control in turn, it looks for a match with its target. As soon
as it matches, it invokes the code. Depending on the context, it will then
either move to the next case or exit structure. If it doesn't find a match and
there is a :default control token, it executes the code for that
token. As usual, if the executed expression does not end in semicolon, it
returns the result for that expression. Otherwise, it returns
One example tells a lot:This
example demonstrates Glee's ability to do standard select
processing. To exercise this simple :select, I run a :for
loop assigning to "i" the numbers 1 to 4 in turn
(:for(1..4[.i]){ ). I use "i" as my selector and
enter a block of cases (:select(i){ ). Each case has a match target
and a block of code to be executed. (e.g.:case(1){'one'} ). If the
selector matches the case's target, the case's code is executed. If there is no
semicolon at the end of the expression, the result is collected into the
sequence result for the select. I have included the optional default case which
is used when no cases match the selector (default{'???'} }. I display
the result in verbose form ( %** ). The :for loop produces the outer
sequence. Each :select creates an inner sequence. In this case it is a one
element sequence. However, :select has a mode ( .all ) which
causes all cases to be executed. In this mode, the :select can meet
more than one case and would thus produce a sequence of more than 1 element.
This example matches the capability of the select or switch
control structure in most programming languages. Glee has more to
offer with its additional modes described later.
Runtime target matching:Most
programming languages, especially non-interpreting languages, will have trouble
with this example. Glee has the capability to do things at run
time that other languages can't. This example illustrates. I begin by creating
two associated sequences containing words in English and Spanish respectively
('one' 'two' 'three' =>english; 'uno' 'dos' =>spanish; ). I take
the English list as an iterator in a :for loop delivering words one at
a time (:for( english[ .word]){). Select uses each word as a selector
( :select( word){ ) and tests it against the list of cases. For
example, (:case( english[1]){ spanish[1]}) tests the word against the
first word in the English list. If it matches, the result is the first word in
the Spanish list. If the word is not found in the English list, (:default{
'unknown'}) fires issuing a diagnostic.Glee collects all the
results of these cases in a sequence which it returns as the result of the
:select block. I save this (}=>x;) and then display it
several ways (x $;); (x %** $;); (x,,'; '$;); and
(x,,\;).