.#0 (Row number)

Since sets in Rax are ordered, it is possible to assign an ordinal number to each tuple in a set. We call this number row number, mimicking the database terminology, and we denote it with .#0. Row numbers can be used in a project or fold expression as shown in the example below.

    `print project[.#0, .#1] {5,1,3};         // Output: {[1, 5], [0, 1], [1, 3]}
    `print project[.#0 % 2, ($).#1] {5,1,3};  // Output: {[1,"5"], [0,"1"], [1,"3"]}
    `print fold[/sum(.#0)] {1,1,1};           // Output: 6
    {#}: A := {6,9,12,3,16,7,8,5,2};
    `print fold [/max(.#0), .#1 % 3] A;       // Output: {[4, 0], [6, 1], [9, 2]}
    `print ["aap", 4.5, (@)"now"].#0;         // Will generate an error.
    `print A ><:[.#0 % 2 != 0] A;             // Will generate an error.
           

In the second line in the above example the row number is used in an expression, .#0 % 2 in this case. This kind of usage can come in handy to generate background colors or labels. In the same example, at the third and the fifth line the row number is used in an aggregate function. Note that the last two lines in the example above will generate an error: "Row number .#0 is not allowed here." In both cases, it would be hard to determine what a suitable value would be.

Besides in a project or fold expression, the .#0 can be used in an order-by tuple list, as shown in the example below.

    {$}: A := {"aap", "noot", "mies"};

    A := A![.#1];         // Sort alphabetically.
    `print A;             // Output: {"aap", "mies", "noot"}
    `print A![.#0];       // Output: {"aap", "mies", "noot"}
         

At the first glance, the order-by-row-number expression, ![.#0], does not seem to have any effect. By definition, .#0 is the ordinal number of the current ordering, so sorting on it would not change the order. There is, however, a fundamental difference between the values printed in the last two lines of the above example. The difference just does not affect the `print expression. However, it will affect expressions where not just the effective order but also the order type is important. Or most any expression that returns an ordered set. An example of where ordering on .#0 does have an effect is given below.

    {$}: S := {"a", "q", "g", "b"};
    {$}: T := S![-.#1];
    `print S \/ S;      // Output: {"a","q","g","b","a","q","g","b"}
    `print T \/ T;      // Output: {"q","q","g","g","b","b","a","a"}
    `print S \/ T;      // Output: {"a","a","b","b","g","g","q","q"}
    T := T![.#0];       //
    `print T \/ T;      // Output: {"q","g","b","a","q","g","b","a"}
    `print S \/ T;      // Output: {"a","q","g","b","q","g","b","a"}
         

The first two lines of the above example setup two sets containing the same four letters (strings), though ordered differently. The set S is in given order, where as set T is in alphabetical descending order. The resulting order of the four lines that contain expressions with the set union, \/ are dictated by the ordering of their left and right arguments. The expression S \/ S is in given order, because both left and right argument are in given order. Likewise, the expression on the fourth line, T \/ T, is in reverse alphabetical order, because both the left and the right argument are in that order. The expression on the line below that, S \/ T, is in natural order (alphabetical) because the left and right arguments have different ordering. On the third line from the bottom in above example, T is "reordered" on its row number, which does not change the effective ordering. However, the ordering is no longer descending alphabetically, but given. As if T had been assigned to by the literal set {"a","g","b","a"}. This means that the expression on the last-but-one line, T \/ T, will be in given order. The same goes for the expression on the last line of above example S \/ T. It might seem tempting to use .#0 in more elaborate ordering schemes. For example, like ![.#1,.#0]. This however, will cause Rax to throw an error. In an order-by expression, .#0 can only be used as a singleton. The only variation is that a - might be used to reverse the order, as in the expression A![-.#0].