General: Glee exhibits much of its power in its ability to manipulate elements of its data objects. In this section, the principle methods of manipulating these elements is presented, usually for each object. The principal objects are strings, numerics, bit vectors, and sequences.
A principal requirement for indexing is the generation of
indices. Conventional languages typically do this with for loops.
Vector languages can attain efficiencies by preparing indices in advance. There
are several operators to facilitate this.
([n1..n2]) Range: The
range operator ( .. ) creates an index vector (vector of numeric
integer elements). The increment of all ranges is 1. You can do math operations
on these vectors to achieve any increments you need. Ranges can ascend or
descend, can have variables as their start and/or end points, and can be
implicitly catenated.
( ` )Index
generator:The most common indices run from 1 to some "n" (usually
the number of elements in the object). The index generator produces such index
vectors .
( `)Index of: This is
a table lookup. But rather than returning data, it returns an index to the
data. The lowest index where the element occurs is returned. If the element is
not found, 0 is returned. The number of elements in the result is the number of
elements in the right argument.
(``)Indices of:When
elements may appear more than once in a vector and you want the indices to all
locations, this is the operator to use. It always returns a sequence of numeric
vectors. That sequence contains the same number of elements as the right
argument. If an element on the right is not found on the left, a NULL vector is
returned in that position of the result.
Indices of Group (
``& ): A very common need in programming is to group chunks of
data by some key. Glee is very flexible. Any object can be a key
or vector of keys. The ("``&"... read as indices of
each) operator produces a sequence of numeric index vectors. Using these
vectors you can index into the original data and pull out information by
groups. It is analogous to using a grade vector to sort various objects
according to some key vector. These examples illustrate the behavior better
than words can describe it.
(=)Unique: This
operator proceeds through the argument weeding out extraneous occurrences of
elements.
(->)Last: The last
(i.e. far right) element of the vector is returned. If the vector contains no
elements, the result is a single element vector containing a nil
element.
(->)Right
Take/Drop: Takes are used to align data. The direction of the operator
indicates the direction of the take. The right argument specifies the number of
elements for the result. If more elements are taken than exist, the far
opposite element is replicated. If the vector contains no elements, the result
if filled with nils of the object type. If a negative right argument is
given, this operator becomes a drop. The negative number specifies the
number of elements to be dropped from the end shown by the direction of the
operator. If all elements are dropped (or more elements than the vector
contains), a NULL vector (vector with 0 elements) results. When
Glee requires argument conformity (i.e. same number of elements
on each side), it does right takes for numerics and left takes for other
objects on the shorter vector to match up the lengths.
(<-)First: The first
(i.e. far left) element of the vector is returned. If the vector contains no
elements, the result is a single element vector containing a nil
element.
(<-)Left Take/Drop:
Takes (or Drops) elements from the left end of the vector filling with elements
from the right. If the vector is empty to begin with, filling is with
nils
(>>)Grade
Up:Grading a vector is like sorting. But rather than rearrange the
elements, the indices used for rearranging them is returned. This is useful if
a number of vectors are in correspondence with each other (e.g. Account Number
vector and a Balance vector). In these cases, you can get the grade indices for
one of the vectors and use it to rearrange both vectors. Glee
uses a stable sort algorithm. This means that it preserves the original order
of elements if they are identical. This is useful if you're doing successive
grades (e.g. you grade by amounts and then by names ... you will preserve the
amounts ordering within the names).
(<<)Grade Down:
This operator returns indices for sorting elements of the vector in descending
order. See Grade up for details.
(>>)Right
Shift: Shift elements to the right in the vector and pull on the objects
nil element from the left. There is no nil element for bits so 0
is used as the fill element. The result always has the same number of elements
as the left argument.
(<<)Left
Shift:Shift elements to the left in the vector. See Right Shift for
details.
(>>>)Sort up:
Sorts the elements in ascending order. This is a convenience operator. It
uses the grade up and index by operators to form the result in
one step. The rules for the grades apply to the sorts.
(<<<)Sort
down: Sorts the elements in descending order. See Sort up and
Grade Up for details.
(|->|)Right
Rotate;|<-|)Left Rotate: The rotate operators rotate elements
of a vector around in a loop. These operators have limited usefulness but serve
to fill out the complete set of Glee gymnastics.
(|->|) and
(|<-|) Flip: Completely reverse the order of the elements. This
operator is useful for changing grades from up to down without the expense of a
complete new sort.
(~) Without:This is a
powerful operator that removes elements from vectors.
(/#)Count Each:Some
operators return sequences of vectors (e.g. Indices of does this). It is
often useful to count the number of elements in each of the vectors in the
sequence. This is the operator for doing that.