|Pliant talk forum
Discussion: The 'each' instruction
|Message posted by marcus on 2003/01/26 21:29:05
|It's about time for me to document the 'each' instruction.
I've noticed that this instruction is already documented in the Dictionary
data type. However, I am not sure whether the 'each' instruction so frequently
used in db applications follows the same syntax.
Therefore, I need two things, a detailed specification of the syntax of
the 'each' instruction (all possible parameters), and the respective intuitive
semantics. Some details on how it actually works would be enlightening.
|Message posted by pom on 2003/01/27 10:29:55
|There are actually four definitions of the meta 'each', that is:
four partial implementations of 'each', focused on different data types.
All of them have not implemented all the features.
The main implementation seems to be the one concerning database pointer
(/pliant/pliant/appli/database/pointer.pli). It implements
'filter' 'sort' 'reversed'.
In /pliant/pliant/language/type/set/each.pli, you find the implementation
for typed List,Array,Index and Dictionary, with allowed keywords 'filter' and
The file /pliant/pliant/language/type/set/relation.pli, which contains the
implementation for Relation data type, allows special keywords 'type' and 'getkeys', while the one in /pliant/pliant/language/type/set/old_dictionary.pli
allows 'type' and 'getkey'.
All of this would need a bit of unification...
The idea of 'each' is to give a tool to traverse a set of elements. The
basic syntax is
each element set
where 'element' is the identifier of a local variable that will be created
by 'each' in order to "point" the data. 'set' is the set to traverse, and
'actions' are the actions to perform.
each item my_list
console item eol
A more complex syntax allows to add some specifiers after the set (and before
the end of line...). Their meaning are:
* filter expression : the expression is compiled (shall cast to a CBool)
and evaluated for each item. An item will be selected only if the expression returns true. For instance, assuming that my_list is a List:Int:
each item my_list filter (item>=3 and item<=5)
console item eol
will select those item which have a value between 3 and 5.
* sort expression: the expression is compiled and evaluated. The items are
sorted according to an increasing value of the expression.
* reversed : means the traversal of the set has to be done the opposite way.
* type t : selects only items with type t (for sets which may include items
with different data types)
* getkey k : has only a meaning for sets with a key. Fills k with the key
associated with the item.
* getkeys k1 k2: has only a meaning for sets which have more than one key. For sets with 2 keys, getkeys k1 k2 fills k1 and k2 with the keys corresponding
to the item.
|Message posted by maybe Marcus on 2003/01/29 22:29:18
|>* sort expression: the expression is compiled and evaluated. The items are
> sorted according to an increasing value of the expression.
A few questions: what do you exactly mean by "The items are
sorted according to an increasing value of the expression" (An example would
Is 'expression' optional? If so, is the set sorted in ascending order?
|Message posted by pom on 2003/01/30 10:24:44
|expression is not optional when you use 'sort' key.
If you want items in ascending order, you should use:
each item my_set sort item
If you want to enumerate items (assumed to be of Str type) in ascending
each item sort item:len
Other example (sorting points with ascending distance to a reference point p0)
each point point_set sort (point:x-p0:x)^2+(point:y-p0:y)^2
|Message posted by maybe Marcus on 2003/02/02 15:38:32
|I have added the missing documentation of the 'each' instruction to the
database engine .page file. Please check for inconsistencies