whichever(T.A : ‘a) 🡒 ‘a, aggregator
The aggregator returns one value for each group if the group is not empty.
table T = with [| as A, as B |] [| "hello", "a" |] [| "hello", "a" |] [| "hello", "b" |] [| "world", "b" |] [| "world", "c" |] table G[gdim] = by T.B where T.B != "c" show table "" a1b4 with gdim whichever(T.A) group by gdim
When the group is empty, the default value for the data type is used.
The result of the
whichever aggregator is deterministic - i.e. running the script twice over the same data yields the same result - but the ordering of the data cannot be relied upon. This aggregator is intended as a faster alternative to the use of
last without introducing an ordering when it is not needed.
whichever, contextual keyword
The contextual keyword
whichever can be used as a modifier for the table creation through the keyword
by. Using the
whichever modifier allows values from the original table to be broadcast into the newly created table. When the modifier
whichever is present, the table creation discards all lines for every group except 1 line arbitrarily chosen.
table T = with [| as Code, as X |] [| "FR", 1 |] [| "US", 2 |] [| "UK", 3 |] [| "FR", 4 |] table Countries[c] = whichever by T.Code Countries.X = T.X // broadcast allowed due to 'whichever' modifier show table "Countries" a1b3 with c, Countries.X // the line 'X == 4' is dropped
In the above script, removing the
whichever modifier prevents the script from compiling as the broadcast
Countries.X = T.X is rejected.
In practice, it is frequently expected that the
whichever by construct would happen in a situation where the choice of the duplicate has no effect because all lines are identical across all the vectors (unlike the example above).